9.3. Displaying Custom Error Messages
Problem
You want to replace the generic messages ASP.NET displays whenever an application error occurs with your own custom error messages.
Solution
Create a web.config file, add the custom
errors element to it, and then create
the custom error pages.
Locate the
web.configfile in the root directory of your application (or create one if it does not already exist).Add a
<customErrors>element to theweb.configfile and add an<error>child element for each custom error page you want to display.Create the custom error pages.
Example 9-5 shows some settings that
we’ve added to a web.config
file to demonstrate this solution.
Discussion
By default, ASP.NET displays its own error page when any of the
standard server errors occurs, such as 401 (access denied), 404 (page
not found), or 500 (internal server error). But a default ASP.NET
error page will not match the look and feel of your application and
may not provide the information you want to convey to your users.
ASP.NET provides the ability, via the web.config
file, to output your own custom error pages. A similar capability is
available in IIS, but customizing the web.config
file is much simpler. Also, because the customization is done in the
web.config file, moving it to another server is
as simple as copying the web.config file and the
custom error pages to the new location.
First, add a <customErrors> element to your
web.config file as a child of
<system.web>. The mode attribute defines when and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access