Search code examples
c#asp.nethttpmodule

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?


Solution

  • You can explicitly set the status code to 404 like:

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

    Response will stop executing.