ASP.NET

Error Pages

As we've seen throughout the tour of ASP.NET, one of the main goals has always been to isolate as much of the Web development facility as possible into ASP.NET. At this point, IIS is really only a middle manager in the scheme of things. Many facilities previously handled by IIS are now handled by ASP.NET. One of those facilities is managing custom error pages. In ASP.NET, you may introduce custom error pages (instead of the client being bombarded with ASP.NET error messages).

To tell ASP.NET to display a particular page upon encountering errors anywhere within your application, just tweak the Web.Config file. Table 16-2 shows the custom error attributes for Web.Config.

Table 16-2 Web.Config values for setting error pages.

Attribute

Description

on/off

on == display custom pages

off == display ASP.NET error pages

defaultRedirect

Direct users here in the event of an exception

remoteOnly

Display custom errors to client, display ASP.NET errors locally

The following example illustrates how to work with custom error pages.

Work with Error Pages

In this example, you'll add some error pages to your application and see what conditions cause them to show.

  1. Open the DebugORama project.

  2. Add a new Web form named ThrowErrors.aspx to the DebugORama application.

  3. Add two buttons: one to throw 404 errors (the nearly ubiquitous "object not found" error) and one to throw other exceptions.

  4. Add two HTML pages to your application to act as custom error pages. Name one page 404 Error.html and the other SomethingBadHappened.html.

    Graphic
  5. Tell ASP.NET to use the error pages by adding the customErrors section to Web.Config, like so:

       <configuration>
          <system.web>
             <customErrors
                defaultRedirect='SomethingBadHappened.htm'mode='On'>
                <error statusCode='404'
                   redirect='404Error.htm'/>
          <customErrors>
       </system.web>
    </configuration>
    

    This tells ASP.NET to show the 404Error.htm page when a file isn't found. ASP.NET will show SomethingBadHappened.htm for any other error.

  6. Now add handlers to generate the errors. Handle the 404 error button by directing the client to a nonexistent page. Handle the second error generator by throwing a random exception.

    public partial class ThrowErrors : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
       protected void Throw404_Click(object sender, EventArgs e)
       {
          this.Response.Redirect("NotThere.aspx");
       }
       protected void ThrowOther_Click(object sender, EventArgs e)
       {
          throw new Exception();
       }
    }
    

    When you try to redirect to a nonexistent file, the "not found" error page shows:

    Graphic

    Throwing a generic exception will cause the other page to show.

    Graphic

Before leaving debugging and diagnostics, let's take a look at trapping exceptions in a more graceful way.