Global Exception Handling with ASP.NET, Page 2
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.
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>
About This Article
This article is excerpted from Debugging ASP.NET by Jonathan Goodyear, Brian Peek, and Brad Fox (New Riders Publishing
2001, ISBN 0-7357-1141-0). Refer to Chapter 4, "Code Structure That Eases
Debugging," for more detailed information on the material covered in this
© Copyright New Riders Publishing, All Rights Reserved
# # #
Page 2 of 2