Gunth
Gunth

Reputation: 11

Asp.net mvc routing : ActionLink return an url with parameters in querystring

I try to have an URL like this /Forum/Index/2

for url I have a route {controller}/{action}/{page} in my global.asax

If I test the above URL with the Route Debugger it corresponds to the above route ( and some other but this is the first one in the list )

but if I create an url with the ActionLink ( like this : <%= Html.ActionLink(item.Title, "Index", new { controller = "Forum", page = page })%> ), this methode return me this URL /Forum/Index?page=2

Is there a way to have an URL with nothing in querystring with the ActionLink method?

This are all my routes and the match route is named DefaultWithPager :

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.IgnoreRoute("google884930bf56853ce4.html");

routes.MapRoute(
    "sitemapXML",
    "Sitemap.xml",                                                                                      // URL with parameters
    new { controller = "Sitemap", action = "Sitemap" }                                                  // Parameter defaults
);

routes.MapRoute(
    "ImageStat",                                                                                        // Route name
    "{controller}/{action}/{MailID}/{UserID}.gif",                                                      // URL with parameters
    new { controller = "", action = "", MailID = "", UserID = "" },                                     // Parameter defaults
    new { 
        MailID = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$", 
        UserID = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" 
    }
);

routes.MapRoute(
    "Default",                                                                                          // Route name
    "{controller}/{action}",                                                                            // URL with parameters
    new { controller = "Home", action = "Index" }                                                       // Parameter defaults
);

routes.MapRoute(
    "DefaultWithPager",                                                                                 // Route name
    "{controller}/{action}/{page}",                                                                     // URL with parameters
    new { controller = "Home", action = "", page = "" },                                                // Parameter defaults
    new { page = @"^\d+$" }  
);

routes.MapRoute(
    "DefaultWithID",                                                                                    // Route name
    "{controller}/{action}/{ID}",                                                                       // URL with parameters
    new { controller = "Home", action = "", ID = "" },                                                  // Parameter defaults
    new { ID = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" }
);

routes.MapRoute(
    "DetailWithID",                                                                                     // Route name
    "{controller}/{action}/{ID}/{Title}",                                                               // URL with parameters
    new { controller = "Home", action = "", ID = "", Title = "" },                                      // Parameter defaults
    new { ID = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" }
);

//Handler route
routes.MapRoute(
    "ImageResizer",                                                                                     // Route name
    "{controller}/{action}/{Title}/{ID}.jpg",                                                           // URL with parameters
    new { controller = "", action = "", Title = "", ID = "" }                                           // Parameter defaults
);


//Section Pages Routes
routes.MapRoute(
    "SectionIndex",                                                                                     // Route name
    "{sectionName}/{controller}/{action}",                                                              // URL with parameters
    new { controller = "", action = "", sectionName = "" }                                              // Parameter defaults                
);

routes.MapRoute(
    "SectionDetails",                                                                                   // Route name
    "{sectionName}/{controller}/{action}/{ID}",                                                         // URL with parameters
    new { controller = "", action = "", sectionName = "", ID = "" }                                     // Parameter default
);

routes.MapRoute(
    "SectionDetailsWithDate",                                                                           // Route name
    "{sectionName}/{controller}/{action}/{month}/{year}",                                               // URL with parameters
    new { controller = "Home", action = "", page = "", month = "", year = "" },                         // Parameter defaults
    new { month = @"^\d+$", year = @"^\d+$" }
);

routes.MapRoute(
    "SectionDetailsWithTitle",                                                                          // Route name
    "{sectionName}/{controller}/{action}/{ID}/{Title}",                                                 // URL with parameters
    new { controller = "", action = "", sectionName = "", ID = "", Title = "" }                         // Parameter defaults
);

routes.MapRoute(
    "SectionDetailsPagingWithTitle",                                                                    // Route name
    "{sectionName}/{controller}/{action}/{page}/{ID}/{Title}",                                          // URL with parameters
    new { controller = "", action = "", sectionName = "", page = "", ID = "", Title = "" }              // Parameter defaults
);

routes.MapRoute(
    "SectionDetailsPaging",                                                                             // Route name
    "{sectionName}/{controller}/{action}/{page}/{ID}",                                                  // URL with parameters
    new { controller = "", action = "", sectionName = "", page = "", ID = "" }                          // Parameter defaults
);

Gauthier

Upvotes: 1

Views: 3216

Answers (2)

Mo09890
Mo09890

Reputation: 174

The way Mvc routing works is that it evaluates each of the routes in order and will use the first one that matches. In your case the first one to match will be the:

routes.MapRoute(
    "Default",
    "{controller}/{action}",
    new { controller = "Home", action = "Index" }
);

As that route doesn't have any mappings for page it will append it on to the url as get parameters.

If you want it to hit the one with the page mapping that should come before any other routes that will match the route data you're passing in.

It should always go from most to least specific.

Upvotes: 0

Bryan
Bryan

Reputation: 535

It sounds like your route registration is just fine. You just need to use the Html.RouteLink helper and tell it the name of the route to use:

<%= Html.RouteLink(item.Title, "DefaultWithPager", new { controller = "Forum", page = page })%>

Upvotes: 4

Related Questions