How to end request with 404 in an IHttpModule?

I'm writing a new IHttpModule. I would like to invalidate certain requests with 404 using a BeginRequest event handler. How do I terminate the request and return a 404?


You can explicitly set the status code to 404 like:

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

Response will stop executing.


你可以试试

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/43786.html

上一篇: ASP.net自定义Http模块

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