- Implementing the Application_Error Event Handler
- Setting Up the Custom Error Page
- About This Article
Setting Up the Custom Error Page
The first step in setting up a custom error page is to modify your config.web file to route the users of your web application to a friendly error page if an exception occurs. It helps to boost users' confidence in your site when it can recover gracefully from the unexpected. Add the code in Listing 5 to the config.web file of your web application.
Listing 5: Adding the <customerrors> Tag to Your Config.web File
<configuration> <customerrors mode="On" defaultredirect="error.aspx" /> </configuration>
Note that your config.web file might already have a <customerrors> tag, so you might only need to modify the existing one. The mode attribute of the <customerrors> tag has three settings: On, Off, and RemoteOnly. If the mode is On, users will always be redirected to the custom error page specified by the defaultredirect attribute if an unhandled exception occurs. If the mode is Off, the details of any exception that occurs will be shown to the user in the browser. The RemoteOnly mode is a hybrid of the two other modes. If you are browsing your web application while sitting at the web server itself, it behaves like the Off mode. All other browsers of the site will get the behavior of the On mode. If no defaultredirect attribute is set, a default ASP.NET "friendly, yet not so friendly" message will be displayed to the user when exceptions occur.
Next, you need to build the custom error page (error.aspx) referenced in the config.web file. This is just an ordinary ASP.NET page that includes helpful information for the user of your web application if an error occurs. An extremely simple example is the one in Listing 6.
Listing 6: Simple Custom Error Page
<html> <head> <title>My web application: Error page</title> </head> <body> An unexpected error occurred in the application. Please contact [ccc] customer service at (800)555-5555. Or, you can click [ccc] <a href="home.aspx">here</a> to go back to the homepage. [ccc] Thank you for your patience. </body> </html>