« Home | Handling Application_Error Method » | Error Handling in .net 3.5 » | Using LINQ in String Collections » | SQL in .net Using LINQ » | Querying the DataTable in .Net » | Converting DataView into DataTable » | Deleting TFS Project »

Error Handling in Web.Config

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.

1. Open the Global.asax file from the previous example.
2. Comment out the Server.ClearError line to ensure that the error surfaces in the Web.config file.
3. Save your changes to Global.asax. Your code should now appear similar to the following:


using System.Diagnostics;

protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error Caught in Application_Error event\n" +
"Error in: " + Request.Url.ToString() +
"\nError Message:" + objErr.Message.ToString() +
"\nStack Trace:" + objErr.StackTrace.ToString();
EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
//Server.ClearError();
//additional actions...
}


4. Add the following code to the <customerrors> section to redirect the user to a custom page:

<customerrors defaultredirect="http://hostName/applicationName/errorStatus.htm" mode="On">
</customerrors>



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 child element allows for this option. For example:

<customerrors defaultredirect="http://hostName/applicationName/errorStatus.htm" mode="On">
<error redirect="filenotfound.htm" statuscode="404">
</error>

</customerrors>

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.

Labels: , , , , ,

Post a Comment