How To Setup A Custom 404 Error Handler In .NET
Java Programming For The Absolute Beginner
|
Subjects > Computers > Software > Programming > Web Application Programming For .NET
By Garnet
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
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
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:
|
Interested in Columbian Drug Trade Killing Many More Americans Annually Than Any War?