我想從遠程客戶端插入,因爲我需要通過http發送數據。
我可以用httpClient
api/performances?date={0}
MVC 4 Web Api Post
我想問一下,如果我的postPorformances()
implemntation我PerformancesController
裏面corrrect正確使用getPerformances()
,如果它是如何從一個客戶端調用?
這是我實現:
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;}
}
我已經試過這一個,但我注意到肯定:
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);
如果ap /演出電話需要很長時間,您可能需要設置客戶端。在撥打電話之前超時 – BlackTigerX 2015-11-19 23:46:23