首页 > 技术文章 > 陨石坑之webapi 使用filter中如何结束请求流

shi-meng 2015-07-10 14:49 原文

先看下正常的结束asp.net 请求流怎么写的

System.Web.HttpContext.Current.Response.Write(“end”); 
 System.Web.HttpContext.Current.Response.End(); 

这样写呢,对调用方看来也是可以的,因为收到的返回数据就是end ,可是我们调代码的时候 就会发现,请求流并没有结束,代码接着往下走了。。。 真正的结束请求流并没有起效果。

那么该如何结束webapi的请求流呢?(需要参考webapi如何使用filter

很简单!

public override void OnActionExecuting(HttpActionContext actionContext)
        {
             actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
        }

 

推荐阅读