【发布时间】:2014-04-07 11:20:03
【问题描述】:
我已经使用以下 put 方法创建了一个 Web-api
public HttpResponseMessage Put(int id, [FromBody]DataModel 模型)
在 put 方法中,我传递了对象,它在数据库中得到更新。它的工作我已经用提琴手检查过。
现在在我的 MVC 应用程序中,我使用以下代码调用它
[HttpPost]
public JsonResult OrderSearch(DataModel model)
{
UpdateOrder(model).Wait();
if (putresult != null && putresult != string.Empty)
{
return Json(putresult);
}
else
{
return Json("Error in getting result");
}
}
private async Task UpdateOrder(DataModel model)
{
string json = JsonConvert.SerializeObject(model);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PutAsync("api/values/"+ model.OrderNo,new StringContent(json)).Result;
if (response.IsSuccessStatusCode)
{
putresult = await response.Content.ReadAsAsync<string>();
}
}
}
但是代码没有命中我在服务上的 Put 方法,putresult 保持空白。我尝试搜索有关 PutAsync 的用法,但找不到任何东西,所以请帮忙。
【问题讨论】:
-
您正在使用 put to a post 方法?
-
当用户编辑他想要的信息时,OrderSearch 是一个部分视图,他点击提交按钮并发布数据,现在由于数据不是新的,它已经可用,所以我正在使用 put 更新上一个。
标签: asp.net asp.net-mvc-4 visual-studio-2012 asp.net-web-api dotnet-httpclient