Home / ASP.NET Wiki / .NET Framework Essentials / Logging Errors and Exceptions in ASP.NET 2.0 / Custom Error messages using Global.asax and Membership model / Quick Error messages with Global.asax

Quick Error messages with Global.asax

 Rate It (7)

Error pages without any effort

void Application_Error(object sender, EventArgs e)

{

Server.Transfer("/error.aspx"); //Displays the error page

}

In error.aspx add this to the content area.

<p>We&#39;re sorry, but the <%=Server.GetLastError().Message.ToLower() %></p>

Thats it. Easy!

Revision number 1, Wednesday, August 11, 2010 1:57:16 PM by lykasd

Comments

I find it a useful to create some type of static class to capture the error in the application_error section to log it and then to some type of server transfer. The method on the static class was also wrapped into a try/catch to make sure that it didn't throw an error itself.. One of the things that you must remember, which might not be too obvious here, is that you need to make sure in your application_error method that you DONT TRANSFER if the page throwing the error is also the destination page. I was called in to troubleshoot a problem where they were doing just that. The error was being thrown in their master page and the error page was derived from that. So they pretty much had an endless loop.

Technically you can easily do the transfer in the web.config but placing it in the Global.asax make it more easier and flexible. thnx

This Code is for C# or VB

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. francissvk (1)
  2. deepeshsp (1)