
How to Define Custom Error Pages for Error Handling In ASP.Net ApplicationWhen 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> 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: 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: Inside
your web page specify the name of custom error page: 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> 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 doesnt 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> You can define
as many error elements as required. Here is a sample code for customPageNotFoundErrorPage.aspx: <HTML>
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |