Missing Pages


One of the classic problems that occur far too often is that of missing pages – that dreaded 404 error message:

click to expand

There are many reasons for this happening, but the three most common are:

  • You've mistyped a URL in your code.

  • You've re-written your site, and people still have links or bookmarks to old pages.

  • People mistyped a URL in the browser.

The first is easily fixed, and should never really be a problem in a live site. The second reason in this list is a problem that can be tackled. If you are rewriting a site to the extent that the links will change or pages will be removed permanently, you should always ensure you have pages that redirect from the old structure to the new structure. There's really not a lot we can do if users end up mistyping URLs though – if you could remotely control people's typing you'd be a millionaire by now. However, it would be better to show a nice page, rather than the generic error page shown above.

In ASP.NET this can easily be handled, but it doesn't work if you are using the Web Matrix Web Server. That's because the Web Matrix Server is designed for the development environment. What you need is Microsoft Internet Information Server (IIS), which is available on Windows 2000 (all versions) and Windows XP Professional. It's not installed by default on either Windows 2000 Professional or Windows XP Professional, but it is included on the installation CDs for these two operating systems as an optional add-on component. (It is installed by default on Windows 2000 Server and higher). If you haven't got IIS, or just plain don't want to install it, then don't completely skip this section. It's worth reading through so that you know what's possible, and how to do this when you need to. If you do want to install IIS, then the instructions are in Appendix A.

The example below requires an IIS Application to be created. If you're unfamiliar with configuring IIS, then take a look at the instructions in the Appendix. We configured a new Virtual Root called Wrox pointing at the directory C:\Wrox, where we have our sample code.

Try It Out—A Custom 'Missing Page' Page

  1. Create a new file, picking the Web.Config template from the (General) tab, and put it in the Wrox application root (in our example C:\Wrox).

  2. Scroll down until you find the customErrors comment section that looks like this:

     <!-- The <customErrors> section enables configuration of what to do if/when an  unhandled error occurs during the execution of a request. Specifically, it  enables developers to configure html error pages to be displayed in place of  a error stack trace: <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">  <error statusCode="403" redirect="NoAccess.htm"/>  <error statusCode="404" redirect="FileNotFound.htm"/> <customErrors> --> 

  3. The lines at the top and bottom of the above code indicate that the lines of code between the <! and > are commented out. These commented-out lines exist to show you an example of the structure, leaving the implementation to you. We need to add some code here, so add the following below the comment block:

     <customErrors mode="On">  <error statusCode="404" redirect="FileNotFound.aspx"/> </customErrors> 
  4. Create a new ASP.NET File called FileNotFound.aspx.

  5. Add some text about a missing page:

    click to expand

  6. Save the page.

  7. Open your browser and type http://localhost/wrox/whatever.aspx in the address bar. Notice how instead of the unfriendly message, we now have our own nice message.

How It Works

The way this works is quite simple. The customErrors section in the configuration file allows us to define what happens under certain error conditions. By turning this On we are telling ASP.NET not to display the standard error details, but to perform a custom action. In our case we have the following:

<customErrors mode="On">   <error statusCode="404" redirect="FileNotFound.aspx"/> </customErrors>

The error tag allows us to indicate a particular error code, and the file that should be shown if that error code occurs. Code 404 is the standard "page not found" error. So, if IIS doesn't find a page it returns this 404 error code, and ASP.NET shows our file instead.

The great thing about this is that you don't have to add any special coding to handle this. You just create your error pages, and change the configuration – ASP.NET handles everything else.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net