【发布时间】:2013-04-30 17:35:10
【问题描述】:
我有一些用于此操作的 api 控制器:
public class ProxyController : ApiController {
public async Task<HttpResponseMessage> PostActionAsync(string confirmKey)
{
return await Task<HttpResponseMessage>.Factory.StartNew( () =>
{
var result = GetSomeResult(confirmKey);
return Request.CreateResponse(HttpStatusCode.Created, result);
});
}
}
这是我的 api 路由配置:
routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
当我尝试对此操作发出任何发布/获取请求时,它会返回“404”错误。我该如何解决?此控制器中的所有其他非异步操作都可以正常工作。
UPD。 JS查询:
$.ajax({
url: Url + '/api/Proxy/PostActionAsync',
type: 'POST',
data: { confirmKey: that.confirmKey },
dataType: 'json',
xhrFields: { withCredentials: true },
success: function (data) {
............
},
error: function (jqXHR, textStatus, errorThrown) {
............
}
});
UPD。通过将[FromBody] 添加到我的操作方法中的参数来解决,就像在 J. Steen 的回答中一样,现在看起来像
public class ProxyController : ApiController {
public async Task<HttpResponseMessage> PostActionAsync([FromBody]string confirmKey)
{
var someModel = new SomeResultModel(User.Identity.Name);
await Task.Factory.StartNew(() => someModel.PrepareModel(confirmKey));
return Request.CreateResponse(HttpStatusCode.OK, someModel);
}
}
而且它有效!
【问题讨论】:
-
也发布您的
ActionResult方法和控制器名称。如果可能的话,你的异步调用 Jquery 也是。:) -
@IgorKonopko:尝试用
[HttpPost]标记动作 -
应该是action-based路由,这样就好了
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-web-api