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 ?id=1&foo=bar
type urls. Not nice.
I have 4 routes defined thusly
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" });
I have tried to order them from most specific to least.
The first 'blog' route works fine and I can use a URL like /blog/list/2/5
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:
public ActionResult BarX(int id)
{
//some stuff
}
public ActionResult BarY(string title)
{
//some stuff
}
I would expect it to use the third route and produce a URL like /foo/barX/3
.
Yet if I use
@Html.ActionLink("TEST1", "barX", "foo", new { id = 3 }, null)
the URL generated is
/foo/barx?id=3
Similarly the URL generated for
@Html.ActionLink("TEST2", "barY", "foo", new { title = "test" }, null)
is
/foo/bary?title=test
So I guess my question is: why are they producing URLs with the old ?id=
syntax and not /foo/barx/3
?