So one of my pet peeves in web development is mixed case URL’s. And I usually make sure that all my URL’s in my personal projects follow this standard. I also believe, contrary to my URL case standard, that my code should follow standards .NET naming techniques, such as Pascal casing for my method names.
These two naming standards come in to conflict with MVC because the name of the action method in the controller is used in its native Pascal case. Which generates URL’s that look like this:
/Home/Index
/Home/About
However I want them to be generated like this:
/home/index
/home/about
So I developed my own Route based off of the System.Web.Routing.Route to force everything to lowercase.
public class LowercaseRoute : System.Web.Routing.Route
{
public LowercaseRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler) { }
public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(url, defaults, routeHandler) { }
public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
: base(url, defaults, constraints, routeHandler) { }
public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
: base(url, defaults, constraints, dataTokens, routeHandler) { }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData path = base.GetVirtualPath(requestContext, values);
if (path != null)
path.VirtualPath = path.VirtualPath.ToLowerInvariant();
return path;
}
}
For anybody as anal as me about stupid stuff such as casing of URL’s you should find this class up above a welcomed addition to your MVC projects.
.NET, C#, MVC, Web

I really hadn’t tought about that one, but I completely agree about correctly casing the URLs. Thanks for sharing the code.
Can you show where you are calling the LowercaseRoute method?
thanks
Good question! I’d also like (need) to know. I am working on an ASP.NET MVC project after working with Rails for a year and was looking for just such a solution. With named routes, we can have the case be whatever we want. But I won’t always be using named routes, so I need the various iterations of Html.ActionLink to convert controller and action names to lowercase. Does this class fix that? Cheers.
I realise this is a long time since the previous comment, but in case anybody else finds this page and also wonders how to use the LowercaseRoute: what I’ve done is to create my own version ASP.NET MVC’s RouteCollectionExtensions:
public static class RouteCollectionExtensions
{
public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults)
{
return MapLowercaseRoute(routes, name, url, defaults, null);
}
public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults, object constraints)
{
if (routes == null)
throw new ArgumentNullException(“routes”);
if (url == null)
throw new ArgumentNullException(“url”);
LowercaseRoute route = new LowercaseRoute(url, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(defaults),
Constraints = new RouteValueDictionary(constraints),
DataTokens = new RouteValueDictionary()
};
routes.Add(name, route);
return route;
}
}
Thank you this is very helpful. I know this class has been widely used, so your extensions will help out a lot.
Is there any license for code posted here by you Nick, and also the extensions @ Tim?
I would like to use both in an OS project if that is OK?
Great stuff Nick (and Tim).
I tweaked Nick’s code just a bit as to avoid lowercasing any querystrings that may be present. I also added a new extension class to provide support for areas.
https://bitbucket.org/LeeDumond/force-mvc-route-urls-to-lowercase-with-support-for-areas
Exactly what I was looking for, thanks for doing my work for me
Doesn’t ASP.NET 2 support this by default?
ASP.NET MVC 2, of course