"routes.LowercaseUrls = true;" does not work? "routes.LowercaseUrls = true;" does not work? asp.net asp.net

"routes.LowercaseUrls = true;" does not work?


This is bug related to MVC4 and will be fixed in MVC5 release. Routes.LowercaseUrls does not affect areas. More info here.

Meanwhile you can use LowercaseRoutesMVC or LowercaseRoutesMVC4 if you need WebApi support.


I tried Several attempts to get this particular boolean flag to work with an MVC3 project with no luck. The ONLY way I could get it to work was to create an MVC4 application project and set the flag in the RouteConfig.cs file in the app start. The really bad part is it lowercased the urls across the site automatically for me until I added an area, then it broke everywhere. Once I excluded the newly added area from the project and reran, the urls were lowercased again.

Something is wonkey with the use of that flag. I would recommend downloading the nuget package for lowercasing urls. It seems as if they haven't quite worked out the kinks in this part of the new framework.

Sorry I couldn't be of more help.


UPDATE: IN AN MVC4 application

Create a new blank MVC4 application and add an Area Called Test, with a Test.cshtml View and a TestController.cs controller.

So I figured out something... though I am not sure if it's a reasonable solution. After playing with the route registration routines, having the areas in the project doesn't break the lowercase functionality.

namespace MvcApplication1.Areas.Test{public class TestAreaRegistration : AreaRegistration{    public override string AreaName    {        get        {            return "Test";        }    }    public override void RegisterArea(AreaRegistrationContext context)    {        //This line breaks the functionality in the area registration.        context.MapRoute(            "Test_default", // Route name            "Test/{controller}/{action}/{id}", // URL with parameters            new { controller = "Test", action = "Index", id = "" }, // Parameter defaults            new string[] { "MvcApplication1.Areas.Test.Controllers" } //namespace            );    }}}

A workaround:

Comment out the lines

        //context.Routes.LowercaseUrls = true;        //context.MapRoute(        //    "Test_default", // Route name        //    "Test/{controller}/{action}/{id}", // URL with parameters        //    new { controller = "Test", action = "Index", id = "" }, // Parameter defaults        //    new string[] { "MvcApplication1.Areas.Test.Controllers" } //namespace        //    );

In RouteConfig.cs

namespace MvcApplication1{public class RouteConfig{    public static void RegisterRoutes(RouteCollection routes)    {        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");        routes.LowercaseUrls = true;        routes.MapRoute(            "Default",             "{controller}/{action}/{id}",             new { controller = "Home", action = "Index", id = UrlParameter.Optional }        );        routes.MapRoute(            "Test_default", // Route name            "Test/{controller}/{action}/{id}", // URL with parameters            new { controller = "Test", action = "Index", id = "" }, // Parameter defaults            new string[] { "MvcApplication1.Areas.Test.Controllers" } //namespace            );    }}}

In The Area Controller Action Method

    public ActionResult Index()    {        // Key if statement to make sure the area maps correctly        if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))        {            this.ControllerContext.RouteData.DataTokens.Add("area", "Test");        }        return View("Test");    }

Resulting HTML for the links in the main page of the project

 <ul id="menu">   <li><a href="/">Home</a></li>   <li><a href="/home/about">About</a></li>   <li><a href="/home/contact">Contact</a></li>   <li><a href="/test?area=Test">Test</a></li> </ul>

Notice however the query string variables are not lowercased and it is not an seo friendly url. However it does find the view. This is as close as I've been able to come using that flag and having the urls go to lowercase.