31 Mar 2008

Force MVC Route URL to Lowercase

16 Comments Uncategorized

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.

Tags: , , ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.

16 Responses to “Force MVC Route URL to Lowercase”

  1. Reply Lorenz Cuno Klopfenstein says:

    I really hadn’t tought about that one, but I completely agree about correctly casing the URLs. Thanks for sharing the code. :)

  2. Reply Kyle says:

    Can you show where you are calling the LowercaseRoute method?

    thanks

  3. Reply YK says:

    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.

  4. Reply Tim Jones says:

    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;
    }
    }

  5. Reply Lee Dumond says:

    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

  6. Reply Lewis Barclay says:

    Exactly what I was looking for, thanks for doing my work for me ;)

  7. Reply ktos says:

    Doesn’t ASP.NET 2 support this by default?

  8. Reply ktos says:

    ASP.NET MVC 2, of course

Leave a Reply