Tuesday, November 6, 2012

Tips for ASP.NET MVC 4: lowercase URLs

A small article about a new feature in ASP.NET MVC 4: the new LowercaseUrls property... (not exactly a new feature of ASP.NET MVC 4: the routing is a part of the .NET Framework itself. The LowercaseUrls property is available in all the projects which use .NET 4.5, whether it’s ASP.NET MVC 3 or ASP.NET MVC 4...)

The problem with ASP.NET MVC is that URLs generated thanks to the routing mechanism are based on the controller and action names. These will typically be resulting in URLs like /Account/Login, etc.
But the routing mechanism is not case-sensitive and a request to account/login works just the same.
It's not a good practice for the SEO to allow URLs with any casing because it could technically be considered to be duplicate pages with the same content.

Usage is very simple, just set the property like in the following code:

   1:  public static void RegisterRoutes(RouteCollection routes)
   2:  {
   3:      ...
   4:      routes.LowercaseUrls = true;
   5:      ...
   6:  }

You can test the modification with a simple action link:

   1:  @Html.ActionLink("Home", "Index", "Home")

An you will see this result:

   1:  <a href="/home/index">Home</a>

You don't have to worry about the capital letters in your URLs but this is not a reason to forget some good SEO practices: you can see some tips here.

6 comments:

  1. Did you notice that as soon as you add an Area to the project, the urls are propercase again? http://stackoverflow.com/questions/13271048/mvc-4-routecollection-lowercaseurls-breaks-when-using-area

    ReplyDelete
  2. Hi Marcel,

    I have not yet tested it with Areas... Thank you for the info!

    While you wait for a fix, you can try this package: http://lowercaseroutesmvc.codeplex.com/

    ReplyDelete
  3. Also please have a look at this package: https://www.nuget.org/packages/LowercaseDashedRoute/
    And read the one-line configuration here: https://github.com/AtaS/lowercase-dashed-route

    ReplyDelete

Note: Only a member of this blog may post a comment.