Working with Custom Error Pages Using ASP.NET
page 4 of 7
Average Rating: 
Views (Total / Last 10 Days): 65850/ 174

Understanding Web.config File

<customErrors> section is responsible for handling the error on the basis of the setting made in that section if we do not call Server.ClearError, trap the error in the Page_Error or Application_Error event handler.  In the <customErrors> section we 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.  We 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 the application (i.e. either in page level or in application level), this custom page is displayed.

In the following code, Server.ClearError() ; is commented in the Application_Error event of  the Global.asax file so that Server.ClearError is never called.  This means the error will be handled on the basis of the setting made in <customErrors> section in the Web.config file.

Listing 7

using System.Diagnostics;
protected void Application_Error(object sender, EventArgs e)
  Exception objError = Server.GetLastError().GetBaseException();
  string strError = "Error Has Been Caught in Application_Error event\n" +
    "Error in: " + Request.Url.ToString() + "\nError Message:" +
    objError.Message.ToString() + "\nStack Trace:" +
  EventLog.WriteEntry("CustomError", strError, EventLogEntryType.Error);

Add the following code to the <customErrors> section to redirect the user to a custom page.

Listing 8

<customErrors defaultRedirect="http://hostName/applicationName/CustomError.htm" mode="On">

We should 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 CustomError.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 CustomError.htm.

Listing 9

<HEAD>Custom Error Page</HEAD>
<H1><FONT COLOR=RED>There is a problem in site. Please try again later. </FONT> </H1>

It should be noted that a custom error page may be a simple HTML file or an ASPX file; it depends upon requirements.

If we run ApplicationError.aspx in the browser, we will see that when the error is thrown we are redirected to the CustomError.htm page.

In the above code listing 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 anyone is developing the application, he or she most likely wants to see the actual ASP.NET error messages and does not want to be redirected to the more user-friendly error page.  The mode attribute includes the following settings or options.

On: This option specifies that custom errors are enabled.  If no defaultRedirect is specified, users see a generic error.

Off: This option specifies that custom errors are disabled.  This allows the display of detailed errors.

RemoteOnly: This option specifies that custom errors are shown only to remote clients and ASP.NET errors are shown to the local host.  This is the default.

Note that defaultRedirect optional attribute specifies the default URL to direct a browser if an error occurs.  When defaultRedirect is not specified, a generic error is displayed instead.  The URL may be absolute (for instance, http://localhost/CustomErrorPage.htm) or it may be relative.  A relative URL such as /CustomErrorPage.htm is relative to the Web.config file that specified the defaultRedirect URL and not to the Web page in which the error occurred.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-02-25 12:08:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search