我正在尝试返回 HTTP 状态代码201 Created
使用 ASP.NET 4 Web API 进行 RESTful POST 操作,但我总是得到200 OK
.
我目前正在 IIS 7.5.7600.16385、VS 2010 Professional、Windows 7 64 位 Professional 上进行调试。
public MyResource Post(MyResource myResource)
{
MyResource createdResource;
...
HttpResponse response = HttpContext.Current.Response;
response.ClearHeaders(); // added this later, no luck
response.ClearContent(); // added this later, no luck
response.StatusCode = (int)HttpStatusCode.Created;
SetCrossOriginHeaders(response);
return createdResource;
}
我见过其他例子HttpContext.Current.Response.StatusCode
在返回数据之前设置,所以我认为这不会是一个问题。我还没有在 MVC 4 源代码中找到它。
(这与我的调查有关这个问题但不同的主题足以保证它自己的问题)
我不确定问题是出在 IIS 还是 Web API。我将做进一步的测试来缩小范围。
HttpContext.Current
是过去的宿醉。
您需要将响应定义为HttpResponseMessage<T>
:
public HttpResponseMessage<MyResource> Post(MyResource myResource)
{
.... // set the myResource
return new HttpResponseMessage<MyResource>(myResource)
{
StatusCode = HttpStatusCode.Created
};
}
UPDATE
正如您可能从评论中注意到的,这种方法适用于beta发布。Not the RC.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)