MVC3 Routes with Different Parameter Types

jonn

New Member
I am struggling to get my head around routing in MVC3.Previously I have generally just avoided the whole area and stuck with ugly old \[code\]?id=1&foo=bar\[/code\] type urls. Not nice.I have 4 routes defined thusly\[code\]routes.MapRoute("Blog", "{controller}/{action}/{PageNumber}/{PostsPerPage}", new { controller = "blog", action = "list", PageNumber = UrlParameter.Optional, PostsPerPage = UrlParameter.Optional });routes.MapRoute("Code", "{controller}/{action}/{title}", new { });routes.MapRoute("Id", "{controller}/{action}/{id}", new { });routes.MapRoute("Default", "{controller}/{action}", new { controller = "home", action = "index" });\[/code\]I have tried to order them from most specific to least.The first 'blog' route works fine and I can use a URL like \[code\]/blog/list/2/5\[/code\] and it maps correctly to my controller.The default route at the bottom is also working as I would expect.However if I have action methods like this:\[code\]public ActionResult BarX(int id){ //some stuff}public ActionResult BarY(string title){ //some stuff}\[/code\]I would expect it to use the third route and produce a URL like \[code\]/foo/barX/3\[/code\].Yet if I use \[code\]@Html.ActionLink("TEST1", "barX", "foo", new { id = 3 }, null)\[/code\]the URL generated is \[code\]/foo/barx?id=3\[/code\]Similarly the URL generated for \[code\]@Html.ActionLink("TEST2", "barY", "foo", new { title = "test" }, null)\[/code\]is\[code\]/foo/bary?title=test\[/code\]So I guess my question is: why are they producing URLs with the old \[code\]?id=\[/code\] syntax and not \[code\]/foo/barx/3\[/code\]?
 
Back
Top