我希望能进行远程客户端数据的插入,因此需要通过http发送数据。
我可以使用
我想询问我的
这是我的实现内容:
我试过这个,但不确定:
我可以使用
httpClient
api/performances?date={0}
以正确地调用getPerformances()
。我想询问我的
PerformancesController
中postPerformances()
的实现是否正确,如果正确,如何从客户端调用它?这是我的实现内容:
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);