我定义了以下两条路线:

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
                );


我正在ProductsController正在使用的视图中使用此代码生成URL:Url.Action("List", new {page=p, category=Model.CurrentCategory})

使用当前配置,我得到以下URL:/ Riding / Page2

但是,如果我从第一条路线中省略了默认的page参数,则会获得以下URL:/?category = Riding&page = 2

在我看来,Url.Action()可以同时匹配这两个路由,并且如果我在第一条路由中指定了默认的页面参数,则决定使用第二条路由,但是如果省略了该参数,则选择使用第一条路由。

鉴于我为页面参数提供了一个值,为什么从路由的默认值中删除它会导致我得到的URL有所不同?

谢谢!

最佳答案

尝试安装NuGet软件包Glimpse。它具有出色的路由调试功能,应该可以为您提供帮助。

这是Scott Hanselan关于如何使用它的博客文章:NuGet Package of the Week #5

关于c# - ASP.NET MVC 3网址 Action 匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5741078/

10-12 12:43
查看更多