Search code examples
asp.nethttp-headershttpmodule

Invalid HttpStatus Code returned in the HttpModule


When I execute a fake url i.e http://localhost:1166/urldoesnotexist, within my HttpModule I get response.StatusCode as 200 (OK). I was expecting it to return 404 (Not Found). Please see the code below

public class HttpPeformanceMonitorModule : IHttpModule
{      
    public void Init(HttpApplication context)
    {
        context.EndRequest += (sender, e) => TraceRequestEnd(sender, "PageLifeCycleTimer", "Begin - End Request");            
    }

    private void TraceRequestEnd(object sender, string timerKey, string title)
    {
        HttpContext httpContext = ((HttpApplication)sender).Context;

        if (response.StatusCode == 200) //for "http://localhost:1166/urldoesnotexist" I get Status Code  200!!!!
        {
             //do stuff...
        }
    } 

}

However once the response has been passed into the browser, the last result that I see on the page is 404 Page Not found. - Which is correct

Can anyone please explain me why the response.StatusCode returns 200 for the fake url ?


Solution

  • What is the actual status code sent by the server, e.g. if you use another tool such as fiddler.

    A lot of pages out there send a 200 when they should send a 404. You can find a lot by doing a search for "page not found", though these days there's a fewer number of them and a larger number of useful pages about 404 pages (an improvement on the part of the search engines, not on the part of the average website).