asp.net-mvc-4 – 无法让ASP.NET 4 Web API返回状态代码“201 –
发布时间:2020-12-16 04:25:57 所属栏目:asp.Net 来源:网络整理
导读:我试图使用ASP.NET 4 Web API为RESTful POST操作返回HTTP状态代码201,但我总是得到200 OK. 我目前正在调试IIS 7.5.7600.16385,VS 2010 Professional,Windows 7 64位专业版. public MyResource Post(MyResource myResource){ MyResource createdResource; ...
我试图使用ASP.NET 4 Web API为RESTful POST操作返回HTTP状态代码201,但我总是得到200 OK.
我目前正在调试IIS 7.5.7600.16385,VS 2010 Professional,Windows 7 64位专业版. 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源代码中跟踪它. (这与我对this question的调查有关,但有足够的主题来保证自己的问题) 我不确定问题是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 正如您在评论中可能注意到的那样,此方法适用于测试版.不是RC. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC响应过滤器OutputCache属性
- asp.net – 如何将两个模型传递给一个View
- asp.net jquery无刷新分页插件(jquery.pagination.js)
- asp.net – FormView无法显示
- asp.net-mvc – 如何启用创建,同时还禁用Kendo Grid的编辑
- asp.net-mvc – 使用DisplayAttribute和自定义资源提供程序
- asp.net-mvc-4 – MVC4如何设置cookie然后重定向到一个动作
- ASP.NET身份验证登录和退出浏览器返回按钮
- 如何使用WebMatrix在ASP.NET网页中创建自定义错误页面?
- asp.net – 如何在SQL Server存储过程中使用逗号分隔(拆分)