Tuesday, November 08, 2005

More often lately I have wanted to handle "nice" URLs in my web application by forwarding those requests on to their actual URLs. ASP.NET offers HttpContext.RewritePath that you can use inside your Global class (HttpApplication descendant) to change one URL to the other. But what about those requests that IIS never hands to ASP.NET because no .aspx or similar extension is included in the URL? This blog discusses what I have recently learned about how to take total control over URL redirection, without any third-party software at all.

The first step is to insert some code into your Global class to check for the old URLs. This can be done in Application_BeginRequest or Application_Error. The latter is good for when you only want the special handling to occur after ASP.NET has already tried to find the URL being sought. If you are expecting these redirected URLs to come in frequently, you probably will want to use Application_BeginRequest.

Suppose you wanted to change all incoming requests for /a.aspx to /b.aspx. This is how you could do it:

You can imagine how to create variants of this to suit your purposes. There are even HttpHandlers that you can write or download, and install in your Web.config file that allow you to declaratively list lots of substitutions using regular expressions. One such that I know of is available from CodeProject.

For my purposes I wanted to handle only those requests that were not found already by ASP.NET, so rather than Application_BeginRequest, I implemented Application_Error, and it looked like this:

Notice how I still handle application errors in the traditional way if my URL rewriting fails to find a match.

The shortcoming of using only this method of redirection is that you are relying on IIS calling up ASP.NET to handle the request, which it only does if it recognizes an ASP.NET extension in the URL (like a.aspx). What if you want to handle requests made to just /a, as in http://www.mycompany.com/a? Well you could make a directory in your web site called "a", and put a default.aspx file in it. I personally have far too many (and end user controlled) keywords like "a" to create folders for them all. So I had to find another solution.

First, let's prepare your web app to handle a spoofed page called 404.aspx. This imaginary page will invoke the ASP.NET interpreter when IIS calls for it, and rewrite the URL as needed. Add some variation of this code to your Global class.

Now we need to get IIS or Apache to pass all these not found requests to ASP.NET, even if those URLs don't include .aspx in them.

If you are using IIS, instruct it to send 404 error pages to your custom /404.aspx URL, by following these steps:

Open up your web site Properties box in IIS

Custom Errors tab

Select the row starting with "404" (not the ones that say 404;2 or 404;3).
Click Edit...

Copy the existing 404 filename to the clipboard. Open the file in Visual Studio. It will look like a 404 page your browser displays when you hit a non-existant page. You'll use this later, so keep this file open.

Back in the "Edit Custom Error Propeties" box, change "Message type" from "File" to "URL"

In the URL box, type in "/404.aspx". Note the .aspx ending, which will force IIS to start ASP.NET even on those URLs that IIS cannot find on the hard disk.

Click OK on each box until you return to IIS.

If you are running mod_mono and Apache together, getting total control is as easy as "SetHandler mono" in your configuration file. Suddenly all requests to that virtual host go through the mod_mono interpreter, even .gif and other static files. When this is done, you can just stick your code into your Global.BeginRequest method and have everything right there. You don't even need to handle 404's.

I hope you find this useful. It took several hours of research on my part, and thanks to other web resources I was able to figure it out. I brought it all together here so you could learn it all at once.