How to Define Custom Error Pages for Error Handling In ASP.Net Application

When you develop your ASP.NET Web application, you will be careful in testing it for errors. However there might be some unhandled situations which when tried by User may end up in errors.


The default error page displayed to the User by your application will be ad-hoc showing all stack trace and source code line number details of the error occurrence. This error page will be useful to developers but not to the Users. Users will always expect meaningful error messages. How to provide such meaningful error messages? You can do it by defining custom error pages.

How to Activate Custom Error Page?

When an error occurs, your application has to realize that custom error page has to be displayed and not the abrupt error page with stack trace details of the error. To display custom error page, make the following entry in web.config file:

<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="customErrorPage.htm" />
</system.web>
</configuration>

When an unhandled error occurs, User will be redirected to customErrorPage.htm. The custom error page mentioned in defaultRedirect attribute can either be an html file or an ASP.NET web page. You can set the mode attribute of customErrors element to three different values; the purpose of each value is mentioned below:

• mode = “On”: Both Local Users as well as Remote Users will view custom error page on occurrence of an unhandled error.

• mode = “Off”: Both Local Users as well as Remote Users will view the default error page containing error details and the stack trace. In this case, defaultRedirect attribute is not necessary. Even if it is provided, it will not have any impact.

• mode = “RemoteOnly”: Only Remote Users will view the custom error page. Local Users will still view the default error page containing error details and the stack trace.

By default, mode is RemoteOnly.

customErrorPage.htm page you create has to provide appropriate error message so that User is not dazed with junk of error messages containing line numbers. Sample code for customErrorPage.htm is mentioned below:
<HTML>
<BODY>
<b>Unexpected Error has occurred. Please Try Again Later. <br> </b>
</BODY>
</HTML>

Setting Custom Error Page at Page Level:

If you want to trigger custom error page at page level then perform the following steps:

• Include the following entry in web.config file:
<configuration>
<system.web>
<customErrors mode="On" />
</system.web>
</configuration>

• Inside your web page specify the name of custom error page:
testWebForm1.ErrorPage="customErrorPageForForm1.aspx"

• Similarly, include the following statement in another web page: testWebForm1.ErrorPage="customErrorPageForForm2.aspx"

When an unhandled error occurs in testWebForm1 then customErrorPageForForm1.aspx will be displayed and when unhandled error occurs in testWebForm2, customErrorPageForForm2.aspx will be triggered.

Setting Custom Error Page at Application Level:

Irrespective of individual pages, if a common custom error page has to be displayed when an unhandled error occurs in any page of the application then make the following entry in web.config file:

<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="defaultCustomErrorPage.htm" />
</system.web>
</configuration>

This will display defaultCustomErrorPage.htm to Remote Users for any unhandled error occurrence in the application. Local Users will view default error details with stack trace. If you want the custom error page to be displayed for Local Users as well then set mode as “On” instead of “RemoteOnly”.

Assume that the above setting is done in web.config file. What if your testWebForm1 contains the following entry?

testWebForm1.ErrorPage="customErrorPageForForm1.aspx"

This page level custom error page setting will override the default setting made in web.config file. When an error occurs on testWebForm1, customErrorPageForForm1.aspx will be displayed. When an error occurs in any other page of the application which doesn’t have its ErrorPage attribute set, defaultCustomErrorPage.htm will be displayed.

Setting Custom Error Page Based on Specific Error Occurrence:

Apart from page level and application level settings, what if you want to display a different customized error page for a specific error? For example when the page requested by User does not exist, instead of displaying your default custom error page it will really be better if you display more customized error page exclusively stating that page could not be found.

You can achieve this by including the child element “error” inside “customError” tag. Here is a sample web.config file defining custom error page for page not found exception:

<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="defaultCustomErrorPage.htm">
<error statusCode="404" redirect="customPageNotFoundErrorPage.aspx" />
</customErrors>
</system.web>
</configuration>

You can define as many error elements as required. Here is a sample code for customPageNotFoundErrorPage.aspx:

<HTML>
<BODY>
<b>Page you are trying to access is currently unavailable. Please try again later. <br> </b>
</BODY>
</HTML>

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

|How to Define Custom Error Pages for Error Handling In ASP.Net Application| Role of Web gardens and web farms in ASP.NET | Understanding Purpose and Usage of "event" Class Member Modifier in C# (C Sharp) | Understanding the Structure and Content of Web.Config File | Usage of C# (C Sharp) Query Keywords – group, by, into | Usage of C# (C Sharp) Query Keywords – join, on, equals, let |Usage of C# (C Sharp) Query Keywords – orderby, ascending, descending | Usage of C# (C Sharp) Query Keywords – select, from, where, in | Usage of Lambda Operator (=>) in C# (C Sharp) |Using C# (C Sharp) as a tool for object oriented programming | Using Dictionaries in .Net – An Overview | Using WCF for providing web service |

 


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.