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.