Can I handle all errors through ASP.Net?

687 Views Asked by At

I am trying to create a 404 handling page but I am now stuck with the page only working for .aspx files, which isn't really what I need.

I am running on IIS6. The site has a wildcard mapping, for extensionless URLs. All requests go through Application_BeginRequest in Global.asax but not all errors go through Application_Error.

Is there a way where I can get the Application_Error to raise for non .aspx files?

This is the code inside the Application_Error

HttpException serverError = (HttpException)Server.GetLastError();

if (serverError != null) {
    int errorCode = serverError.GetHttpCode();
    if (errorCode == 404) {
        Server.ClearError();
        Server.Transfer("/errorPage.aspx");
    }
}
1

There are 1 best solutions below

5
On

The only way I've successfully handled this with IIS 6 is to add a specific handler for 404 in IIS custom errors that points to one of your .aspx or .ashx files (i.e. point it to your errorPage.aspx).