Monday, April 25, 2011

Asp.Net MVC: How do I enable dashes in my urls?

I'd like to have dashes separate words in my URLs. So instead of:

/MyController/MyAction

I'd like:

/My-Controller/My-Action

Is this possible?

From stackoverflow
  • Don't do it. A dash a URL is represented as %2D (just like a space is %20).

    MrChrister : We are matching routes for backwards compatibility. All new URLs are clean, but we don't want to lose old links we are replacing.
  • You could write a custom route that derives from the Route class GetRouteData to strip dashes, but when you call the APIs to generate a URL, you'll have to remember to include the dashes for action name and controller name.

    That shouldn't be too hard.

  • Plenty of websites successfully use hyphens (or dashes or whatever--the key next to my 0) in URLs without any encoding happening. Hyphens are more SEO-friendly than underscores, as well, as Google (and other engines) reads hyphen-delimited strings as multiple terms, whereas underscore-delimited strings are interpreted as a single term.

  • You can use the ActionName attribute like so:

    [ActionName("My-Action")]
    public ActionResult MyAction() {
        return View();
    }
    

    Not sure if you can do something similar for controllers.

    Eduardo Molteni : This is the real answer. Not sure why Phil didn't add this info
    Nicholas Piasecki : Nice tip. Just to add: When you do this with the default View() invocation, MVC will search for "My-Action.aspx" somewhere in the Views folder, not "MyAction.aspx," unless you explicitly specify the original name.
    David Kolar : @Eduardo I think the ActionName attribute was added for Preview 5, which came out just after Phil's post.
    Alex : How do you explicitly specify the view file name? Do I have to change the views file name to My-Action.aspx?
    DaRKoN_ : @Alex, yes, you set the view file name to be whatever the parameter is in the ActionName attribute. "My-Action.aspx" for instance.
  • You could create a custom route handler as shown in this blog:

    http://blog.zero7web.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/

    public class HyphenatedRouteHandler : 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);
            }
        }
    

    ...and the new route:

    routes.Add(
                new Route("{controller}/{action}/{id}", 
                    new RouteValueDictionary(
                        new { controller = "Default", action = "Index", id = "" }),
                        new HyphenatedRouteHandler())
            );
    

    A very similar question was asked here: http://stackoverflow.com/questions/2070890/asp-net-mvc-support-for-urls-with-hyphens

0 comments:

Post a Comment