Recipe 12.4. Displaying Custom Error Messages


Problem

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

Solution

Create a web.config file, add the custom errors element to it, and 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 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 12-5 shows some settings that we've added to a web.config file to demonstrate this solution.

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 simpler. 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 customerror 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 customerror 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.

The defaultRedirect attribute defines the custom error page that will be displayed if an error occurs and there is no specific error element (described later) for the error. By default the defaultRedirect attribute is set to GenericErrorPage.htm. You should change this to your own generic error page.

Next, add an error element for each server error 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 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";

See Also

Search "<customErrors> element" in the MSDN library.

Example 12-5. Custom error settings in web.config

 <?xml version="1.0"?> <configuration>    <system.web>      <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">     <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: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net