我有这个动作有些API控制器:的Asp网页API异步行动 - 404错误
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路由confuguration:
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.斯蒂恩的回答解决了,现在它看起来的一样
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太:) – RajeshKdev 2013-04-29 15:14:25
添加所有需要的 – 2013-04-30 07:23:47
@IgorKonopko:尝试用'[HttpPost]'标记动作' – Catalin 2013-04-30 07:27:22