asp.netasp.net-mvcasp.net-mvc-2

Global.asax Exception Handling and HTTP Error Codes


I have a fairly standard MVC2 app running on 4.0 and under IIS6. I took the wildcard filter approach to route all requests to the asp.net ISAPI filter. So far so good.

I have now added some code to Application_Error in the global.asax to trap exceptions and email me the stack, etc. This also works fine.

But, I am receiving all kinds of 404 related traffic. Mostly "The controller for path '/favicon.ico' was not found or does not implement IController" but I can imagine that I soon be receiving robot.txt requests etc.

Is there any way that I can get the HTTP Error Code from HttpContext.Current.Server.GetLastError()? I can then simply ignore all 404 results. Or is there a different approach that I should be looking at?


Solution

  • Ask a question, find the answer....

    I need to test this but it look promissing.

    var ex = HttpContext.Current.Server.GetLastError();
    
    // filter out 404 responses
    var httpException = ex as HttpException;
    if (httpException != null && httpException.GetHttpCode() == 404)
        return;