Voglio fare un inserimento da un client remoto per quello che ho bisogno di inviare dati via http.
posso usare il getPerformances()
correttamente con un httpClient
api/performances?date={0}
MVC 4 Web Api Post
voglio chiedere se il mio postPorformances()
implemntation dentro il mio PerformancesController
è corrrect e se è come chiamare da un cliente?
Qui è la mia realizzazione:
public class PerformancesController : ApiController
{
// GET api/performances
public IEnumerable<Performance> getPerformances(DateTime date)
{
return DataProvider.Instance.getPerformances(date);
}
public HttpResponseMessage postPerformances(Performance p)
{
DataProvider.Instance.insertPerformance(p);
var response = Request.CreateResponse<Performance>(HttpStatusCode.Created, p);
return response;
}
}
public class Performance {
public int Id {get;set;}
public DateTime Date {get;set;}
public decimal Value {get;set;}
}
ho provato questo, ma io sono sicuro che nota:
private readonly HttpClient _client;
string request = String.Format("api/performances");
var jsonString = "{\"Date\":" + p.Date + ",\"Value\":" + p.Value + "}";
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var message = await _client.PutAsync(request, httpContent);
Se la chiamata di ap/performance richiede molto tempo, è possibile impostare il client.Timeout prima di effettuare la chiamata – BlackTigerX