How can I have lowercase routes in ASP.NET MVC?
Asked Answered
L

8

155

How can I have lowercase, plus underscore if possible, routes in ASP.NET MVC? So that I would have /dinners/details/2 call DinnersController.Details(2) and, if possible, /dinners/more_details/2 call DinnersController.MoreDetails(2)?

All this while still using patterns like {controller}/{action}/{id}.

Labrecque answered 18/5, 2009 at 16:30 Comment(4)
I ended up writing all my routes manually anyway for various reasons and I think it's hard to avoid doing that with anything that's not just CRUD. So I just wrote them in lowercase.Momentous
Using Web Forms? Go here: msdn.microsoft.com/en-us/library/…. (I am gradually converting my project from web forms to MVC and have both in the project)Vonnie
im pretty sure you can do this as defaultBunk
i dont think it matters if you type in the routes in lower or upper case.Bunk
E
254

With System.Web.Routing 4.5 you may implement this straightforward by setting LowercaseUrls property of RouteCollection:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.LowercaseUrls = true;

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Also assuming you are doing this for SEO reasons you want to redirect incoming urls to lowercase (as said in many of the links off this article).

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        //You don't want to redirect on posts, or images/css/js
        bool isGet = HttpContext.Current.Request.RequestType.ToLowerInvariant().Contains("get");
        if (isGet && !HttpContext.Current.Request.Url.AbsolutePath.Contains("."))
        {
            //You don't want to modify URL encoded chars (ex.: %C3%8D that's code to Í accented I) to lowercase, than you need do decode the URL
            string urlLowercase = Request.Url.Scheme + "://" + HttpUtility.UrlDecode(HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
            //You want to consider accented chars in uppercase check
            if (Regex.IsMatch(urlLowercase, @"[A-Z]") || Regex.IsMatch(urlLowercase, @"[ÀÈÌÒÙÁÉÍÓÚÂÊÎÔÛÃÕÄËÏÖÜÝÑ]"))
            {
                //You don't want to change casing on query strings
                urlLowercase = urlLowercase.ToLower() + HttpContext.Current.Request.Url.Query;

                Response.Clear();
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", urlLowercase);
                Response.End();
            }
        }
    }
Eanore answered 18/5, 2009 at 16:30 Comment(11)
This is by far the simplest thing to do in 4.0.Bryonbryony
any way to override on case by case basic for instance upper case SKU or a CamelCase product name?Gisser
Wish this were at the top... Almost cargo-culted a lot of code!Sooty
Great answer :-) The latter SEO part fits nicely into an HTTP module.Ancier
@Aaron Sherman Where is the Application_BeginRequest part supposted to go? It's giving me errors when its inside public class RouteConfig and also when its outside of if.Reproductive
@richard-mišenčík add it to Global.asax fileMallard
@Mallard Thanks, I added it there but now I have just one error: The name 'Regex' does not exist in the current contextReproductive
@RichardMišenčík, you need to add a using statement for it at the top of your Global.asax file. ("using System.Text.RegularExpressions;")Disjoint
@IshmaelSmyrnow Thanks, but I already found that elsewhere so it works nowReproductive
Note that this will alter case sensitive URLs on redirect when using RedirectToRouteResult, causing potential bugsTobit
Cheers to Microsoft for this one liner.Bohemian
N
44

These two tutorials helped when I wanted to do the same thing and work well:

http://www.coderjournal.com/2008/03/force-mvc-route-url-lowercase/ http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

EDIT: For projects with areas, you need to modify the GetVirtualPath() method:

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
  var lowerCaseValues = new RouteValueDictionary();

  foreach (var v in values)
  {
    switch (v.Key.ToUpperInvariant())
    {
      case "ACTION":
      case "AREA":
      case "CONTROLLER":
        lowerCaseValues.Add(v.Key, ((string)v.Value).ToLowerInvariant());
        break;
      default:
        lowerCaseValues.Add(v.Key.ToLowerInvariant(), v.Value);
        break;
    }
  }
  return base.GetVirtualPath(requestContext, lowerCaseValues);
}
Nicolella answered 18/5, 2009 at 16:39 Comment(15)
Actually the Wordpress link (@GoNeale's article) is superior. It provides extension methods for a friendlier registration, and includes handling to redirect incoming requests that aren't in lower case, so that you don't fragment your page ranking in search engines between multiple versions of the same page.Conceited
GONeale link has changed; URL is now goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvcSorce
Does this still apply to MVC3?Almondeyed
@Almondeyed Yes the tutorials are still valid.Nicolella
@Derek - Nope, the tutorials break down when using Area's. After 3 days of trying EVERYTHING... I found a better solution, theres a library called Attribute Routing. Solves the problem and makes life a lot easier. philliphaydon.com/2011/08/…Almondeyed
@Almondeyed I took another look at where I'm using it and sure enough I made a small modification to get it to work with areas in the GetVirtualPath() method.Nicolella
This solution doesnot work for me for areas it still works fine for regular case. All my urls in the area section are still not lower caseMunger
sorry, but how and where do you use the Route subclass? Can you please edit the answer to show where to inject this in the asp.net system?Hangbird
it has error in some cases. if you add some Object to RouteValue, it cann't cast to string, and it throw exception.Mattland
For mvc 4 there is a better solution using property routes.LowercaseUrls = true; More info on dhuvelle.com/2012/11/tips-for-aspnet-mvc-4-lowercase-urls.htmlWombat
@Marc but does not work for areas #13271548Wily
@MarcCals Actually routes.LowercaseUrls comes with .NET 4.5, not MVC 4. Sadly for me.Baelbeer
@GONeale What happened to your URL? Second link is dead now! Made me laugh though, the title is "The Best Gone Ale Site on the Internet"Underlay
You can read goneale.com's article hereArron
@chteuchteu that link didn't work for me, but this one did.Grimsley
W
33

If you happened to use ASP.NET Core, you probably should have a look at this:

Add the following line to the ConfigureServices method of the Startup class.

services.AddRouting(options => options.LowercaseUrls = true);
Wystand answered 18/5, 2009 at 16:30 Comment(1)
Same for Core 2.0. Also at https://mcmap.net/q/152911/-automatically-generate-lowercase-dashed-routes-in-asp-net-corePinzler
U
21

If you are using the UrlHelper to generate the link, you can simply specify the name of the action and controller as lowercase:

itemDelete.NavigateUrl = Url.Action("delete", "photos", new { key = item.Key });

Results in: /media/photos/delete/64 (even though my controller and action are pascal case).

Unappealable answered 18/5, 2009 at 16:30 Comment(2)
I think doing this work in one central location is the easiest and most standard solution. This is just as bad as inline CSS. (Apparently 15 people use inline CSS).Reciprocation
Is it true for Linux servers too?Wilks
P
16

I found this at Nick Berardi’s Coder Journal, but it did not have information on how to implement the LowercaseRoute class. Hence reposting here with additional information.

First extend the Route class to LowercaseRoute

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

Then modify the RegisterRoutes method of Global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add(new LowercaseRoute("{controller}/{action}/{id}", 
        new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }), 
        new MvcRouteHandler()));

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

I would however like to know a way to use routes.MapRoute...

Pirate answered 18/5, 2009 at 16:30 Comment(2)
GONeale's article provides an extension method so you can write routes.MapRouteLowercase(... which is nicer than the above: goneale.wordpress.com/2008/12/19/…Conceited
GONeale's entire blog disappeared. Here is another blog entry with similar content (and the same extension method). It addresses this situation in the context of reducing duplicate content.Clinic
B
11

You can continue use the MapRoute syntax by adding this class as an extension to RouteCollection:

public static class RouteCollectionExtension
{
    public static Route MapRouteLowerCase(this RouteCollection routes, string name, string url, object defaults)
    {
        return routes.MapRouteLowerCase(name, url, defaults, null);
    }

    public static Route MapRouteLowerCase(this RouteCollection routes, string name, string url, object defaults, object constraints)
    {
        Route route = new LowercaseRoute(url, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(defaults),
            Constraints = new RouteValueDictionary(constraints)
        };

        routes.Add(name, route);

        return route;
    }
}

Now you can use in your application's startup "MapRouteLowerCase" instead of "MapRoute":

    public void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Url shortcuts
        routes.MapRouteLowerCase("Home", "", new { controller = "Home", action = "Index" });
        routes.MapRouteLowerCase("Login", "login", new { controller = "Account", action = "Login" });
        routes.MapRouteLowerCase("Logout", "logout", new { controller = "Account", action = "Logout" });

        routes.MapRouteLowerCase(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
    }
Bucharest answered 18/5, 2009 at 16:30 Comment(1)
For anyone reading this, the LowercaseRoute class in the first code snippet above appears to come from this other answerGrimsley
P
5

This actually has two answers:

  1. You can already do this: the route engine does case-insensitive comparison. If you type a lower-case route, it will be routed to the appropriate controller and action.
  2. If you are using controls that generate route links (ActionLink, RouteLink, etc.) they will produce mixed-case links unless you override this default behavior.

You're on your own for the underscores, though...

Piggish answered 18/5, 2009 at 16:48 Comment(0)
Q
3

Could you use the ActionName attribute?

 [ActionName("more_details")]
 public ActionResult MoreDetails(int? page)
 {

 }

I don't think case matters. More_Details, more_DETAILS, mOrE_DeTaILs in the URL all take you to the same Controller Action.

Quotient answered 18/5, 2009 at 17:27 Comment(2)
I haven't tried that - will it let you use either one? ("moredetails" or "more_details")Piggish
To follow up, I tried it and it requires you to use the specified name, so no, it won't allow you to handle it either way. Also, depending how you constructed your controller action and view, you may need to specify the name of the view explicitly.Piggish

© 2022 - 2024 — McMap. All rights reserved.