Choosing the ritgh route when you generate link in aspnet mvc

I’m moving my first steps on asp.net mvc, and in my test project I added a route after the default one in this way.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 RouteTable.Routes.MapRoute(
               "Default",
               "{controller}/{action}/{id}",
               new { controller = "Home", action = "Index", id = "" }
             );
            RouteTable.Routes.MapRoute(
                "PagedController",
                "{controller}/{action}/{pageid}/{id}",
                new { controller = "Home", action = "Index", pageid = 0, id = "" }
              );

As you can see the second route is used when the controller need the concept of current page, it is very close to the default one, but it has another parameter called pageid. Now I generate a link to the paged controller with the following code.

1
2
3
4
5
6
Html.ActionLink(
    "Edit", 
    "ManageAlbum", 
    "PhotoManager", 
    new { pageid = ViewData.Model.CurrentPage, id = Guid.Empty},
    null)

But generated link is PhotoManager/ManageAlbum/00000000-0000-0000-0000-000000000000?pageid=0 that is wrong. The page id was put in querystring and not in the path as I want. The problem is derived from the order of routes, because the ActionLink function scans route from the first to the last. Since the first route match with the parameter the ActionLink method decides to use the Default route, appending the pageid parameter to the querystring.

If you need to manually choose the route you need to generate your link you can use a different method of the Html helper object.

1
2
3
4
5
6
7
8
9
Html.RouteLink(
    "Edit", 
    "PagedController", 
    new { 
        controller=  "ManageAlbum", 
        action = "PhotoManager", 
        pageid = ViewData.Model.CurrentPage, 
        id = Guid.Empty}, 
    null)

As you can see with the RouteLink() method you can choose exactly the route you want to use, and generate desidered link /ManageAlbum/PhotoManager/0/00000000-0000-0000-0000-000000000000.

alk.

Tags: Asp.net MVC