开发者

ASP.NET MVC 3 Url.Action matching

开发者 https://www.devze.com 2023-02-27 06:48 出处:网络
I have the following two routes defined: routes.MapRoute(null, \"\" // ~/ ,new { controller=\"Products\", action=\"List\", page=1 });

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号