views:

386

answers:

3

Hi.

Is it possible to force/extend routing engine generate urls in lower case, "/controller/action", instead of "/Controller/Action"?

+3  A: 

Yes, just change it in the routing in the global.asax file.

@All asking if it matters: Yes I do think it matters. Having the url all in lower case just looks better.

Every time you don't make something look nice when you can, Bill Buxton kills a kitten.

IainMH
Wow, Bill Buxton is HARSH!
SirDemon
I think anyone who has been to Mix or ReMix in the last 12 months has had to sit through Big Billy B's (admitedly quite interesting) rant on the importance of design needing to be baked in to a product.
IainMH
+1  A: 

You can do this by creating another Route class that inherits from GetVirtualPath and then create an extension method to add your new route class to the dictionary.

http://goneale.wordpress.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

ajma
A: 

What's more, you should force any incoming requests that are uppercase to be redirected to the lowercase version. Search engines treat URLs case-sensitively, meaning that if you have multiple links to the same content, that content's page ranking is distributed and hence diluted.

Returning HTTP 301 (Moved Permanently) for such links will cause search engines to 'merge' these links and hence only hold one reference to your content.

Add something like this to your Global.asax.cs file:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // Don't rewrite requests for content (.png, .css) or scripts (.js)
    if (Request.Url.AbsolutePath.Contains("/Content/") ||
        Request.Url.AbsolutePath.Contains("/Scripts/"))
        return;

    // If uppercase chars exist, redirect to a lowercase version
    var url = Request.Url.ToString();
    if (Regex.IsMatch(url, @"[A-Z]"))
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.AddHeader("Location", url.ToLower());
        Response.End();
    }
}
Drew Noakes