Route Sequence in ASP.NET MVC

While testing out some of the new features of the March preview of ASP.NET MVC, I found an interesting feature of the routing behavior.  (alternative suggestions welcome)

Suppose you want to setup the route http://mywebsite/login but your default route in global.asax.cs is:

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

 

How about adding?

  routes.Add(new Route("login", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Security", action = "Login", id = (string)null }),
            });

 

Lest you find yourself throwing things and brushing up on your Jr. High vocabulary, note that the the default behavior (at least for now) is matching in order that the routes are added.  First match wins.  So, if you add the  new route after the first, your url will get caught in the first route pattern and may violate your expectations.

 A great way to look at mvc routes is to download the route debugger which is a dll and one line of debug code to give you this:

 

Posted on 3/25/2008 9:09:00 PM by john

Permalink | Comments (0) | Post RSSRSS comment feed |

Categories: ASP.NET MVC Framework

Tags:

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

May 17. 2008 19:00