Application Level Error Handling

Web.config settings

The web.config file contains a <customErrors> tag to apply generic exception handling and to specify redirection to an error page:

<customErrors defaultRedirect="/error/SiteError.aspx" mode="on">  
 <error statuscode="404" redirect="/error/filenotfound.aspx" />
</customErrors>

As shown in the preceeding code, SiteError.aspx is the default redirect page for site errors not trapped by the application code in all conditions. In development servers, users can set the mode setting to "Off" or "RemoteOnly" to see the full default ASP.NET error message. Setting this to "RemoteOnly" will show the ASP.NET error message on the server, but will show the default redirect page on a remote client.

Although most of these error page settings are also available with IIS, using the Web configuration method allows for easier deployment of the site onto many computers. However, users should also configure the IIS metabase for these error conditions to redirect to the appropriate error page. Also web.config settings will only apply for ASP.NET files (.aspx, .asmx, .ascx, and not .htm or .asp files).

If the <customErrors> tag is missing in the web.config file, ASP.NET errors are redirected to a generic server error page, and the following message is displayed:

An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

Implementation

ASP.NET sends minimal information to a page when an error occurs. A Get method is sent to this page with the query string key "ErrorPage" with the virtual path to the page in which the error occurs. The user interface section of this page uses culture-neutral content to display error information to the user.

Exception Handling

In the Global.asax file, error events at the application level are handled by the event Application_Error, which writes the available information to the event log file. Depending on the customErrors setting in the web.config file the appropriate error page will be displayed.

Page Level Error Handling

@Page directive:

Since the web.config file will be configured to redirect to an error page for unhandled errors, @Page ErrorPage = 'xyz.aspx' should not be used in the application.

Page_Error Event Handling

Implementing the Page_Error event handler in individual feature pages of the site is not recommended. This is because the Page_Error event handler requires a full implementation to render the page completely by itself. Since the web.config file is already configured to show a custom error page it is best to make use of it. The Page_Error event handler should be implemented only when it is required to free up page resources and even then it should not call Server.ClearError so that exception will remain unhandled and will allow ASP.NET to redirect to the Application_Error event handler and to the default error redirect page.

The Page_Error event handler should be implemented only in pages where system errors might occur (for example, the Checkout page because it is running a pipeline), and it is recommended that the Page_Error event handler write the error message to the event log and keep the exception alive so that user will be redirected to the custom error page.

Ee797169.note(en-US,CS.20).gifNote

  • Server.GetLastError can be used to obtain a reference to the unhandled exception object.
  • Server.ClearError is used only when propagation of the exception is to be stopped.

Copyright © 2005 Microsoft Corporation.
All rights reserved.