About this Project

Tired of your MVC application generating mixed-case URLs like http://mysite.com/Home/About or http://mysite.com/Admin/Customers/List? Wouldn't http://mysite.com/home/about and http://mysite.com/admin/customers/list be a lot nicer? I think so too, so I wrote this little project to help make that work.

By default, ASP.NET MVC generates outgoing route URLs that preserve the casing of your controller and action names. Since controllers are classes and actions are methods, these outgoing URL segments will most likely be camel-cased, as that is the standard when naming these constructs. LowercaseRoutesMVC lets you very easily make ASP.NET MVC generate outgoing route URLs that are always lower-cased, without having to rename any of your controller classes or action methods. It includes support for MVC Areas as well. (If the generated outgoing URL contains a querystring, its casing will not be affected.)

There is also a separate beta version with support for lower-casing HTTP Routes generated by the ASP.NET Web API.

Usage Notes

Using this project is very easy. Once added as a reference in your web project, you need only make a minor change to the method calls in your RegisterRoutes method in Global.asax. If you are using Areas in your project, you will also make similar changes in the RegisterArea override(s) in your area registration classes.

Step 1. - Install the library via Nuget

PM> Install-Package LowercaseRoutesMVC or via the NuGet management tools in Visual Studio.

If you need support for lower-casing HTTP Routes, install this version instead:

PM> Install-Package LowercaseRoutesMVC4

Step 2. - Remap Your Main Routes

Go to the RegisterRoutes method in Global.asax. Replace all the calls to MapRoute with MapRouteLowercase.

For example:

using LowercaseRoutesMVC
 ...

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRouteLowercase( // changed from routes.MapRoute
       "Default",
       "{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

Step 3. (if using Areas) - Remap Your Area Routes

Go to the RegisterArea override in the area registration class for each Area in your application. (For example, if you have an Area named Admin, this class will be called AdminAreaRegistration and will be located in the Areas/Admin folder.) Again, replace all the calls to MapRoute with MapRouteLowercase.

For example:

using LowercaseRoutesMVC
 ...

public override void RegisterArea(AreaRegistrationContext context)
{
   context.MapRouteLowercase( // changed from context.MapRoute
       "Admin_default",
       "Admin/{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

Step 4. (if using ASP.NET Web API) - Remap Your HTTP Routes

There is a separate Nuget package called LowercaseRoutesMVC4 that contains all of the functionality above, plus beta support for lower-casing HTTP Routes generated by the ASP.NET Web API. If this is something you're interested in doing, be sure to install the LowercaseRoutesMVC4 Nuget package and not the LowercaseRoutesMVC one. Keep in mind that the ASP.NET Web API is still in beta, and that the routing features are likely to change.

Go to the RegisterRoutes method in Global.asax. Replace all the calls to MapHttpRoute with MapHttpRouteLowercase.

For example:

using LowercaseRoutesMVC4
 ...

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapHttpRouteLowercase( // changed from routes.MapHttpRoute
       "DefaultAPI",
       "api/{controller}/{id}",
       new { id = UrlParameter.Optional }
   );
}

Rebuild your application, and you're done. All the routes generated by your application should now be lowercased!

NOTE: If any of the generated URLs contain querystrings, the casing of the querystring will NOT be altered.

Pro Tip

If you find this little utility handy, you might also want to check out my MVC Route/URL Generation Unit Tester, which provides convenient, easy to use methods that let you unit test the route table in your ASP.NET MVC application. You can use this to assert that your application is properly generating lower case URLs.

Questions? Issues? Suggestions?

Feel free to post them under the Discussions tab above, or hit me up on Twitter @leedumond.

Last edited Apr 29, 2012 at 9:21 PM by LDumond, version 36