【发布时间】:2016-07-21 16:51:30
【问题描述】:
我的所有 Wep Api 2 控制器上的 Options() 动词都出现 CA2000 错误。
在方法“PromotionController.Options()”中,对象“g__initLocal0”并未沿所有异常路径进行处理。
我需要支持为跨域 ajax 调用发出的飞行前请求,所以我所做的只是返回一个 HttpStatusCode.Ok,以便随后的 post/put/delete 将触发。
public HttpResponseMessage Options()
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
我尝试过这样的 using 语句:
public HttpResponseMessage Options()
{
using(var response = new HttpResponseMessage())
{
response.StatusCode = HttpStatusCode.OK;
return response;
}
}
但是当我用 Postman 的 Options 请求点击它时,我得到了一个异常:
无法访问已释放的对象。\r\n对象名称:'System.Net.Http.HttpResponseMessage'。
如何以不会引发代码分析错误的方式返回 HttpStatusCode.Ok?
谢谢!
【问题讨论】:
-
好的,为什么不使用 Owin/CORS 或 WebAPI CORS?
-
@Igor 我在搜索时看到了该帖子,但据我所知,HttpResponseMessage 不支持 RegisterForDispose。只有 HttpRequestMessage 可以。
标签: c# asp.net-web-api2