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

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

Up vote 24 down vote favorite 22 share g+ share fb share tw.

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

Asp. Net-mvc url routes case link|improve this question edited Nov 17 '10 at 22:37 community wiki5 revs, 2 users 60%J. Pablo Fernández.

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. – J.

Pablo Fernández Aug 18 '09 at 12:04.

These two tutorials helped when I wanted to do the same thing and work well: coderjournal.com/2008/03/force-mvc-route... 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); }.

1 Cheers for the mention. – GONeale Jul 30 '09 at 1:30 1 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.

– Drew Noakes Sep 7 '10 at 22:42 7 GONeale link has changed; URL is now goneale.com/2008/12/19/lowercase-route-u... – Daniel Liuzzi Jan 14 '11 at 3:45 4 @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/… – Phill Aug 9 '11 at 22:32 1 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 case – MoXplod Sep 11 '11 at 0:52.

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

1. I think this is the easiest and most standard solution. – André Pena Dec 12 '11 at 19:24.

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

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/… – Drew Noakes Sep 8 '10 at 12:58 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. – patridge Apr 29 '11 at 2:45.

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 ); }.

This actually has two answers: 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. 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...

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

("moredetails" or "more_details") – GalacticCowboy May 18 '09 at 22:07 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. – GalacticCowboy May 19 '09 at 14:08.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions