Tuesday, February 15, 2011

Create a Custom Error Page in 4 steps - .NET - ASP.NET - C#

Visual Studio gives us the possibility to handle errors by using many different types of error handling functions. However, we know we need to tell the viewer that something went wrong when the application crashes, and for this we need to implement an Error page.

I remember seeing some custom error pages in the past that were implemented by using a class that will be called after an exception was thrown.

It worked well, but the implementation of an error page can be more efficient and faster by doing the following:

NOTE: These steps don't cover the creation of the actual page.

1- Open your solution and right-click the project entry In solution explorer.

2- Choose the Global Application File (Global.asax) (if it is not already present in the solution) and click Add.

3- Modify the Application_Error method like the following:

void Application_Error(object sender, EventArgs e)
   // Code that runs when an unhandled error occurs
4- Open the Web.Config file and add the following element as a child of the <System.Web> element

<customErrors mode="RemoteOnly" defaultRedirect="Error_Page.aspx"/>
After the above changes, the remote clients will be forward to the Error_Page.aspx page when unhandled errors are thrown.

No comments:

Post a Comment

Thank you for your thoughts. Your comment will appear in my blog shortly after review.

Have a great day!