Creating an Error Page


ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 12.  Error Handling

If you want to add application-wide error handling so that any unhandled error takes you to a page of your choosing (as opposed to the scary page shown in Figure 12.2), you can make a simple modification in the Web.Config file to add your settings. Open Web.Config and modify the customErrors element, which normally looks like the following:

 <customErrors mode="RemoteOnly" /> 

You can change it so any error that occurs will redirect to a specific page using the defaultRedirect attribute:

 <customErrors mode="RemoteOnly" defaultRedirect="ErrorPage.aspx" /> 

You must create the page you're redirecting users to; that is, ErrorPage.aspx must be a page you've created in your application. To test this out in the sample application, you'll also need to modify the mode attribute by default, this attribute indicates that your error page only takes effect for remote requests (requests coming in from outside the current machine). Because you're browsing to this page locally, set this attribute to On and then try the No Error Handling option on the sample page.

You'll be directed to a very simple page indicating that an error occurred.

Once you've set the attributes correctly, triggering an unhandled exception will redirect you to the error-handling page. (Handled exceptions, of course, don't redirect users that's the point of having exception handling in the first place.)


The three options allowed for the mode attribute are On, Off, and RemoteOnly. If you set this attribute to On, all requests that generate unhandled errors redirect to the specified page. If you set it to Off, you'll only see the default page if an unhandled error occurs (as you saw in Figure 12.2). If you set it to RemoteOnly (the default value), only remote requests will be redirected to the error page.


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234 © 2008-2017.
    If you may any questions please contact us: