Tôi muốn thực hiện chèn từ một khách hàng ở xa mà tôi cần gửi dữ liệu qua http.
Tôi có thể sử dụng một cách chính xác getPerformances()
với một httpClient
api/performances?date={0}
MVC 4 Web Api Post
Tôi muốn hỏi nếu postPorformances()
implemntation tôi bên PerformancesController
của tôi là corrrect và nếu nó là làm thế nào để gọi nó là từ một khách hàng?
Đây là triển khai thực hiện của tôi:
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;}
}
Tôi đã thử cái này nhưng tôi lưu ý chắc chắn:
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);
Nếu cuộc gọi ap/biểu diễn mất nhiều thời gian, bạn có thể muốn đặt ứng dụng khách.Thời gian chờ trước khi thực hiện cuộc gọi – BlackTigerX