Recipe 9.3 Displaying Custom Error Messages


9.3.1 Problem

You want to replace the generic messages ASP.NET displays whenever an application error occurs with your own custom error messages.

9.3.2 Solution

Create a web.config file, add the custom errors element to it, and then create the custom error pages.

  1. Locate the web.config file in the root directory of your application (or create one if it does not already exist).

  2. Add a <customErrors> element to the web.config file and add an <error> child element for each custom error page you want to display.

  3. Create the custom error pages.

Example 9-5 shows some settings that we've added to a web.config file to demonstrate this solution.

9.3.3 Discussion

By default, ASP.NET displays its own error page when any of the standard server errors occurs, such as 401 (access denied ), 404 (page not found), or 500 (internal server error). But a default ASP.NET error page will not match the look and feel of your application and may not provide the information you want to convey to your users. ASP.NET provides the ability, via the web.config file, to output your own custom error pages. A similar capability is available in IIS, but customizing the web.config file is much simpler. Also, because the customization is done in the web.config file, moving it to another server is as simple as copying the web.config file and the custom error pages to the new location.

First, add a <customErrors> element to your web.config file as a child of <system.web> . The mode attribute defines when and where the custom error pages are displayed. Set the mode to RemoteOnly to have the custom error pages displayed only when accessing the application from a remote machine. When set to RemoteOnly , the ASP.NET error pages will not be displayed when accessing the application from the local machine. Set the mode to On to have the custom error messages displayed on local and remote machines. Set the mode to Off to display the ASP.NET error messages on local and remote machines.

Next, add an error element for each server error that you want to redirect to a custom error page. Set the statusCode attribute to the server error code, and set the redirect attribute to the URL of the page to be displayed when the error occurs. You can include parameters in the URL if desired.

When the error is a 404 error (page not found), for example, ASP.NET includes a parameter in the URL to indicate the name of the requested page that was not found. The URL for the redirection of the 404 error just described would be:

 http://   [server]   /ASPNetCookbook/PageNotAvailable.aspx?aspxerrorpath= /ASPNetCookbook/BadPage.aspx 

Your application can use the Request.QueryString collection to retrieve the name of the page that was not found and include the information in your custom page:

 labMessage.Text =  Request.QueryString("aspxerrorpath")  & _ " Is Not Available On This Site" 
 labMessage.Text =  Request.QueryString["aspxerrorpath"]  + " Is Not Available On This Site"; 

Example 9-5. Custom error settings in web.config
 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> ..  <customErrors mode="RemoteOnly">   <error statusCode="404" redirect="PageNotAvailable.aspx"/>  ..  </customErrors>  .. </system.web> </configuration> 

ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 179

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: