开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜