Lower-casing URIs in .NET

A few months ago, a client asked that their .NET site have all its URIs lower-cased for search-engine optimization purposes.

This was an existing site with a lot of files, already in SVN. SVN in Windows is wonky (by which I mean, terribly broken) when it comes to changing the case of filenames, so I decided to go for a programmatic solution to the renaming.

I ended up with the following:


void Application_BeginRequest(object sender, EventArgs e)
{
    string currentURL = Request.RawUrl.ToLower();
    if ((currentURL != Request.RawUrl) && (!currentURL.Contains(".axd")))
    {
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", currentURL);
    }
}

The most important thing is the “!currentURL.Contains(“.axd”)” condition. .NET sticks its scripts in .axd files that are — for some reason — requested in a case-sensitive manner. Leaving out that condition broke all my AJAX and post-backs and caused Javascript errors out the yin-yang.