You want to replace the generic messages ASP.NET displays whenever an application error occurs with your own custom error messages.
Create a web.config file, add the custom errors element to it, and create the custom error pages.
Example 12-5 shows some settings that we've added to a web.config file to demonstrate this solution.
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 simpler. 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 customerror 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 where the custom error pages are displayed. Set the mode to RemoteOnly to have the customerror pages displayed only when accessing the application from a remote machine. When set to RemoteOnly, the ASP.NET error pages will not be displayed when accessing the application from the local machine. Set the mode to On to have the custom error messages displayed on local and remote machines. Set the mode to Off to display the ASP.NET error messages on local and remote machines.
The defaultRedirect attribute defines the custom error page that will be displayed if an error occurs and there is no specific error element (described later) for the error. By default the defaultRedirect attribute is set to GenericErrorPage.htm. You should change this to your own generic error page.
Next, add an error element for each server error you want to redirect to a custom error page. Set the statusCode attribute to the server error code, and set the redirect attribute to the URL of the page to be displayed when the error occurs. You can include parameters in the URL if desired.
When the error is a 404 error (page not found), for example, ASP.NET includes a parameter in the URL to indicate the name of the requested page that was not found. The URL for the redirection of the 404 error described would be:
Your application can use the Request.QueryString collection to retrieve the name of the page that was not found and include the information in your custom page:
Search "<customErrors> element" in the MSDN library.
Example 12-5. Custom error settings in web.config