Microsoft .NET MVC: How to Use Hyphens in URLs for Conrollers and Actions

Once you created your MVC project, surely you can use underscores in in the names of your controllers and actions. Using hyphens in URLs, however, can make a difference for when your site is discovered by search engines. When you use hyphens, search engines consider every word of your URL as a separate term; but if you use underscores that part of URL will become one term, reducing the chances of your page to be discovered greatly.

Given how class names cannot contain hyphens, how to you solve this? The idea is very simple – URLs map to their specific controllers and actions in MVC, and this mapping is being handled by the MvcRouteHandler class. So really all you have to do is to create a subclass and translate your controller and action names into class names that you rather can have.

The first step is to create a class that inherits from MvcRouteHandler:

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace My.Namespace
{
	public class MyCustomMvcRouteHandler : MvcRouteHandler
	{

		protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
		{
			requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
			requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
			return base.GetHttpHandler(requestContext);
		}

	}
}

In the overridden GetHttpHandler method above, all we are doing is simply replacing the hyphens with underscores that can be used in a class name. As you probably already know, the MvcRouteHandler class is instantiated in the beginning of the HTTP request and handles which classes to instantiate based on what data it gets. So, now that we have this translation, name your controllers and views after the hyphen-reach URLs you wish for, replacing the hyphens with underscores.

The one last important step is to specify that you want to use your custom handler for your routes. It is a somewhat pain in the ass since you have to add it to every route mapping – if you know an easier way, please post it!

In your Global.asax file where you map your routes, go ahead and specify your custom handler for each of them:

routes.MapRoute(
                "Home", // Route name
                "{controller}/{action}", // URL with parameters
                new { controller = "Home", action = "Index" } // Parameter defaults
            ).RouteHandler = new MyCustomMvcRouteHandler();

If you don’t get it, the routes.MapRoute() method returns a Route object. Since you only need to assign the RouteHandler property, you can use it in the same statement.

Have fun!

This entry was posted in MVC and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Microsoft .NET MVC: How to Use Hyphens in URLs for Conrollers and Actions

  1. Jacco says:

    But does this also solve the issues with

    Html.ActionLink(…)

    These links always seem to contains to original Controller name.

    • ℓūfħer says:

      @Jacco: Yes, I used this method to have hyphens in both controller and actions names in Html.ActionLink() and it works fine. Though I am using MVC 4, but that shouldn’t make any difference.

Leave a Reply

Your email address will not be published. Required fields are marked *