如何在一个IHttpModule中使用404结束请求?

我在写一个新的IHttpModule。 我想用404使用BeginRequest事件处理程序使某些请求无效。 我如何终止请求并返回404?


你可以显式设置状态码为404,如下所示:

HttpContext.Current.Response.StatusCode = 404; 
HttpContext.Current.Response.End();

响应将停止执行。


你可以试试

throw new HttpException(404, "File Not Found");

另一种可能性是:

HttpContext.Current.Response.StatusCode = 404; 
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
链接地址: http://www.djcxy.com/p/43785.html

上一篇: How to end request with 404 in an IHttpModule?

下一篇: HTTP Response filter can't decode the response bytes the second time