tags:

views:

516

answers:

5

Our groups legacy ASP 3.0 web apps were able to take advantage of a global error file by setting up a custom error file within IIS's Custom Error's tab. I'm unable to find a similar solution for ASP.NET apps.

Does anyone know if there is a way to have a centralized "Error.aspx" page (for example) that will trap errors for an entire application pool? The objective is to avoid adding custom code to each app's Global error handler ....

Any guidance is greatly appreciated!

+1  A: 

You can add the <customErrors defaultRedirect="[url]"></customErrors> tag to your web.config or even up to your machine.config to redirect to a custom error page. The custom errors tag also suppors multiple subelements that can be used to define custom errors.

Just remember when editing the machine.config file, it will affect all the applications on that machine.

SaaS Developer
@SaaS: Thanks for the response! It's basically doing a Response.Redirect when employing <customErrors/>, right? If so, wouldn't the Servers error info get dumped by the time the error page was loaded?
deadbug
+1  A: 

Checkout the Application_Error method of your global.asax.cs file.

You can get the details of the error within here by using something like:

Exception myError = Server.GetLastError();
Exception baseException = myError.GetBaseException();
Ben R
A: 

You an also capture the error by utilizing the Application_OnError event in the Global.asax file. Then, you can do as Ben R stated above. Capture the error via the Server.GetLastError() routine.

Donn Felker
A: 

You can also build a custom module called "ErrorHandlingModule" that would go into HttpModule.

public class ErrorHandlingModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.Error += new System.EventHandler(OnError);
    }

    public void OnError(object obj, EventArgs args)
    {
        Exception ex = HttpContext.Current.Server.GetLastError();
        //Log your error here or pick a funny message to display
        HttpContext.Current.Server.ClearError();
        HttpContext.Current.Response.Redirect("/Error.aspx", false);
    }
}

It would look something like this.

Maxim
+1  A: 

We've used ELMAH to do just what you are asking. You can have it work at the machine level so you don't have to set up custom error handling on each web site:

http://code.google.com/p/elmah/

You could also then incorporate the <customErrors> tag in your web.config to be sure they were redirected to an error page. ELMAH would take care of making sure you were notified about everything.

Micky McQuade
Thanks for the tip Micky! I'll check it out
deadbug