我正在尝试发布到 Asp.Net WebApi 发布方法:
// POST /api/citycode
public HttpResponseMessage<CityCode> Post(CityCode citycode)
{
try
{
Test.SelfTrackingEntities.BusinessLayer.BusinessManagers.CityCodeManager myCityCodeManager = new CityCodeManager(Utility.GetConnectionString());
var id = myCityCodeManager.Create(citycode);
var response = new HttpResponseMessage<Test.SelfTrackingEntities.BusinessLayer.BusinessEntities.CityCode>(citycode) { StatusCode = HttpStatusCode.Created };
response.Headers.Location = new Uri(VirtualPathUtility.AppendTrailingSlash(Request.RequestUri.ToString()) + citycode.Name);
return response;
}
catch (Exception e)
{
var response = new HttpResponseMessage(HttpStatusCode.Conflict);
response.Content = new StringContent(e.Message);
throw new HttpResponseException(response);
}
}
客户电话是:
var objectContent = CreateJsonObjectContent(citycode);
objectContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var requestMessage = new HttpRequestMessage<CityCode>(citycode, jsonMediaType);
return httpClient.PostAsync(addressSuffix, objectContent).ContinueWith(task =>
{
var responseMessage = task.Result;
return responseMessage.Content.ReadAsAsync<CityCode>().ContinueWith(readTask =>
{
return readTask.Result;
});
}).Unwrap();
但是服务器从不接听电话,我使用的是 JsonNetFormatter 而不是内置的格式化程序。