[Home]How To Setup A Custom 404 Error Handler In .NET

Contents | (Visit Preferences to set your user name.) | Related To How To Setup A Custom 404 Error Handler In .NET | RecentChanges | Preferences | Index | Login | Logout

Featured: Featured Stories | Picture Gallery

Napoleonic Wars | Savage Uncensored May 15 2004
Google
Chat11.com Web Bible11.com MyBibleCenter.com
Search For Books About:
/BottomAd1, /Error404, /YourApp, Computers, ErrorDocument, Garnet, Programming, Software
Search The Net:
Garnet
Computers
Software
Programming
ErrorDocument
/Error404
/YourApp
/BottomAd1
Cover of ISBN 0672323079

Cover of ISBN 0761535225

Java Programming For The Absolute Beginner

Cover of ISBN 0596003471

Programming .NET Components

How To Create a 404 Error Handler For Your .NET Application

.NET Web Application Programming

Subjects > Computers > Software > Programming > Web Application Programming For .NET
By Garnet

Use of 404 Error Handlers

I've always liked custom 404 error handlers. I've never liked to give a user a 404 - Page Not Found Error. One of my very earliest websites included a 404 handler that tried to intelligently redirect the user to something related to the wrong URL they'd tried to jump to.

The best 404 error handlers are probably dynamic scripts that try to supply context sensitive information to the user. But handling mistyped urls is only one use of a 404 error handler.

Another improtant use for error 404 handlers is to put friendly urls on top of script generated output. Until recently, some search engines were unable to spider script type urls with parameters. When an error404 handler is used to return web pages for normal URLs, this can make content more easily digested by various web spiders.

Many wikis have URLs that look like this: http://www.chat11.com/cgi-bin/wiki.pl?Programming

By having the wiki program respond as a 404 handler, it is possible to use much simplier URLs such as http://www.chat11.com/Programming

Setting Up The 404 Error Handler

It is easy to configure an Apache webserver to use a 404 error handler. It can be done as simply as including a single line in an .htaccess in an http file directory, such as the root directory of a virtual web host:

ErrorDocument?Create 404 /cgi-bin/wiki.pl

On Microsoft IIS web servers, use the Internet Information Services (IIS6) Manager to configure a 404 handler for any directory, virtual root, or entire website.

Right click on any folder, select Properties. Select the Custom Errors tab. From the list of errors, select HTTP Error 404. Then click on "Edit...". Select message type of URL, then enter path and file of the error 404 handler. With .NET, your error handler page might be named Error404.aspx, so you might enter /Error404?Create.aspx for the file in the root directory. If you are setting up the 404 error handler just for your own application subdirectory, you might enter /YourApp?Create/Error404?Create.aspx

Programming The Error 404 Handler

One of the first things you will want to do is write a small program to show you all the server variables that are available to you from within the error handler script.

Here is some sample C Sharp (C#) to return a string containing all the server variables. Put this code in your Error404.aspx.cs file:

  protected string GetServerVars()
  {  
      string S="";
      int i=1;
      System.Collections.IEnumerator myEnumerator = Request.ServerVariables.AllKeys.GetEnumerator();
      while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
         {
            S=S+String.Format( "[{0}] {1}={2}<br>\n", 
                               i++, myEnumerator.Current,
                               Request.ServerVariables[myEnumerator.Current.ToString()] 
         );
			
       return S;             
   }

It's very simple to put this into your page. Put the following line into your Error404.aspx page:

   <% =GetServerVars() %>

The most interesting variable will be the QUERY_STRING variable:

   [32] QUERY_STRING=404;http://localhost/Hive/HomePage.htm

It is easy to pick this apart, and create a new URI object to use with the URL that generated the 404 error:

   string S=Request.ServerVariables["QUERY_STRING"];
   S=S.Split(new Char[] {';', ' '}, 2)[1];
   Uri QUri=new Uri(S);

Other related issues:

Some articles here about programming web applications for .NET:


Could not find Programming/BottomAd1?Create

Search for books about:

/BottomAd1, /Error404, /YourApp, Computers, ErrorDocument, Garnet, Programming, Software

Search The Net:
Garnet
Computers
Software
Programming
ErrorDocument
/Error404
/YourApp
/BottomAd1

Contents | (Visit Preferences to set your user name.) | Related To How To Setup A Custom 404 Error Handler In .NET | RecentChanges | Preferences | Index | Login | Logout
Edit this www.chat11.com page | View other versions
Last edited April 8, 2007 2:54 am (diff)
Search:
Sign up for PayPal and start accepting credit card payments
instantly.
Bobsgear - Get A Free Enterrpise Wiki Space!
Review: The Bobsgear Project was started to develop a variety of Confluence plugins. This installation of the Confluence Enterprise wiki includes flexible attachments, many Confluence plugins, personal blogs, interesting articles, and more. Bobsgear already has spaces related to politics, art and photography wiki, technical issues wiki, ediscovery wiki, health, Christian theology and Sabbath School wiki, the bible, book reviews, and quotations. Bobsgear allows free signup, and invites anyone to create a free hosted Confluence wiki space.


NEW USERS CLICK HERE! for a quick introduction to Wiki.

 

 Interested in Columbian Drug Trade Killing Many More Americans Annually Than Any War?
2253 total hits since 8/2007
Recently accessed pages: Al Jezeera Book The Art Of Talk Cicada Allergies Cute Ftp Famous Quote Quotation GPM - Gallons Per Minute Google Adsense Top Paying Keywords Google Adwords Psychics HSI ACCT 212-0032 Principles Of Accounting II Handbook For Lent - Fasting New Feature Sandbox Quotations Roma Plastilina For Modeling Clay SimpleWeb Writing/TopAd1

Elapsed:2