March 31st, 2008

Force MVC Route URL to Lowercase

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: , ,

Social: kick it on DotNetKicks.com

This entry was posted on Monday, March 31st, 2008 at 2:45 pm and is filed under ASP.NET, C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

9 Responses to “Force MVC Route URL to Lowercase”

  1. DotNetKicks.com Says:

    Force MVC Route URL to Lowercase…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. 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. :)

  3. Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, .NET, RegEx, .NET, Icons, CSS, UI | Code-Inside Blog Says:

    [...] Force MVC Route URL to Lowercase [...]

  4. Coder Journal’s MVC Toolkit - Nick Berardi’s Coder Journal Says:

    [...] I did a whole post on why I needed this in my toolkit. Mostly because of my obsessions to have all URL’s in [...]

  5. What I learned about SEO from Celebrity Jeopardy! - Nick Berardi’s Coder Journal Says:

    [...] I fall on the side of lowercase letters and hyphens splitting the words: [...]

  6. Makiwa » Blog Archive » Lowercase MVC Route URLs Says:

    [...] Nick Berardi posted a really simple solution. He derived from System.Web.Routing.Route and overrode the GetVirtualPath [...]

  7. ASP.NET MVC Archived Buzz, Page 1 Says:

    [...] to Vote[Del.icio.us] Force MVC Route URL to Lowercase – Nick Berardi’s Coder Journal (10/26/2008)Sunday, October 26, 2008 from [...]

  8. Kyle Says:

    Can you show where you are calling the LowercaseRoute method?

    thanks

  9. 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.

Leave a Reply