简体   繁体   中英

ASP.NET MVC 3 Url.Action matching

I have the following two routes defined:

routes.MapRoute(null, "" // ~/  
                ,new { controller="Products", action="List", page=1 });

routes.MapRoute(null, "{category}/Page{page}" // ~/CategoryName/Page21  
                , new { controller = "Products", action = "List", page = 1 }
                , new { page = @"\d+" } //page must be numerical
                );

I am generating a URL using this code in the view being used by ProductsController: Url.Action("List", new {page=p, category=Model.CurrentCategory})

With the current configuration, I get this URL: /Riding/Page2

However, if I omit the default page parameter from the first route, I get this URL instead: /?category=Riding&page=2

It seems to me that Url.Action() can match both routes and is making a decision to use the second route if I have the default page parameter specified in the first route but is choosing to use the first route if that parameter is omitted.

Given that I am supplying a value for page parameter, why would eliminating it from the route's defaults make a difference in the URL I get back?

Thanks!

Try installing the NuGet package Glimpse . It has excellent route debugging, it should be able to help you.

Here's a blog post by Scott Hanselan on how to use it: NuGet Package of the Week #5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM