Routing error in ASP.NET MVC 4 -


i have stumbled upon curious behavior in asp.net mvc routing.

in routeconfig file, when map route (the default route):

        routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new {                 controller = "home",                 action = "index",                 id = urlparameter.optional             }         ); 

using:

@html.actionlink("index", "home") 

i nice, clean , short url, like: http://mysite/

but if add optional parameter after id, this:

        routes.maproute(             name: "default",             url: "{controller}/{action}/{id}/{name}",             defaults: new {                 controller = "home",                 action = "index",                 id = urlparameter.optional,                 name = urlparameter.optional             }         ); 

the same actionlink outputs url: http://mysite/home/index every time. verified same behavior using redirecttoaction.

my questions are: there way work around , shorter url in later case? why asp.net mvc routing engine behaves differently in these cases?

edit

i managed work around issue following instructions posted dave a. added custom route before "default" route matches custom url pattern.

routes.maproute(     name: "custom",     url: "{controller}/{action}/{id}/{name}",     defaults: new {         controller = "home",         action = "index",         id = urlparameter.optional     } );  routes.maproute(     name: "default",     url: "{controller}/{action}/{id}",     defaults: new {         controller = "home",         action = "index",         id = urlparameter.optional     } ); 

there indeed "bugs" or inefficiencies in way mvc framework handles routes. in case, especially, trying make best use of params getting confused.

read post hacked shows more "glitches" in system http://weblogs.asp.net/imranbaloch/archive/2010/12/26/routing-issue-in-asp-net-mvc-3-rc-2.aspx


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -