Monday, February 7, 2011

How to use the Web.config file

If you do not call Server.ClearError or trap the error in the Page_Error or Application_Error event handler, the error is handled based on the settings in the <customErrors> section of the Web.config file. In the <customErrors> section, you can specify a redirect page as a default error page (defaultRedirect) or specify to a particular page based on the HTTP error code that is raised. You can use this method to customize the error message that the user receives.

If an error occurs that is not trapped at any of the previous levels in your application, this custom page is displayed. This section demonstrates how to modify the Global.asax file so that Server.ClearError is never called. As a result, the error is handled in the Web.config file as the last point to trap the error.

Open the Global.asax file from the previous example.

Comment out the Server.ClearError line to ensure that the error surfaces in the Web.config file.

Save your changes to Global.asax. Your code should now appear similar to the following:

Note You must modify the file path in defaultRedirect attribute so that it references the relevant Web server and application names.

Because the errors that are trapped at this level are sent to a default error page, you must create an error page named ErrorStatus.htm. Keep in mind that you are using this method to control what is presented to the user, so this example uses an .htm page for the error page. Add the following code to ErrorStatus.htm:

<HTML><HEAD><TITLE></TITLE><META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0"></HEAD><BODY> <b>Custom Error page!</b> <br> You have been redirected here from the <customErrors> section of the Web.config file.</BODY></HTML>

To test the code, save the files, build the project, and then view AppEvent.aspx in the browser. Notice that when the error is thrown, you are redirected to the ErrorStatus.htm page.

Although you can reference a default error page in the value of the defaultRedirect attribute in the <customErrors> section, you can also specify a particular page to redirect to based on the HTTP error code that is raised. The <error> child element allows for this option. For example:

Note The page that is specified in defaultRedirect of the <customErrors> section is an .htm file. I

Notice that the <customErrors> section includes a mode attribute that is set to On. The mode attribute is used to control how the error redirection occurs. For example, if you are developing the application, you most likely want to see the actual ASP.NET error messages and do not want to be redirected to the more user-friendly error page. The mode attribute includes the following settings:

On: Unhandled exceptions redirect the user to the specified defaultRedirect page. This mode is used mainly in production.

Off: Users receive the exception information and are not redirected to the defaultRedirect page. This mode is used mainly in development.

RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information. All other users are redirected to the defaultRedirect page. This mode is used mainly for debugging.