![](/img/trans.png)
[英]how do i set the etag using the HttpResponseMessage object in mvc 4 web api?
[英]How do I Dispose a HttpResponseMessage in my Web Api Method?
我在所有Wep Api 2控制器上都遇到了Option2000()動詞的CA2000錯誤。
在方法'PromotionController.Options()'中,未沿所有異常路徑放置對象'<> g__initLocal0'。
我需要支持針對跨域Ajax調用發出的飛行前請求,因此我正在做的只是返回HttpStatusCode.Ok
以便隨后的發布/發布/刪除操作將被觸發。
public HttpResponseMessage Options()
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
我已經嘗試過using語句,例如:
public HttpResponseMessage Options()
{
using(var response = new HttpResponseMessage())
{
response.StatusCode = HttpStatusCode.OK;
return response;
}
}
但是當我用郵遞員的選項請求擊中它時,我得到了一個例外:
無法訪問已處置的對象。\\ r \\ n對象名稱:'System.Net.Http.HttpResponseMessage'。
如何以不會引發代碼分析錯誤的方式返回HttpStatusCode.Ok
?
謝謝!
您可以使用HttpRequestMessage
類的RegisterForDispose
方法RegisterForDispose
要處理的響應對象。 .Net基礎結構將在處理請求后處理所有注冊的對象。
我不能保證在那之后您不會收到錯誤,但是該對象將被處置,以便您可以抑制錯誤。
范例 :
HttpResponseMessage response = Request.CreateResponse();
// Do what you need with response
// ...
// ...
request.RegisterForDispose(response);// response will be disposed by host
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.