简体   繁体   English

ASP.NET MVC 3 Url.Action匹配

[英]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}) 我在ProductsController使用的视图中使用此代码生成URL: Url.Action("List", new {page=p, category=Model.CurrentCategory})

With the current configuration, I get this URL: /Riding/Page2 使用当前配置,我得到以下URL: / Riding / Page2

However, if I omit the default page parameter from the first route, I get this URL instead: /?category=Riding&page=2 但是,如果我省略了第一条路线的默认页面参数,我会改为使用此URL: /?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. 在我看来, Url.Action()可以匹配两个路由,并且如果我在第一个路由中指定了默认页面参数但是如果省略该参数则选择使用第一个路由,则决定使用第二个路由。

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? 鉴于我正在为页面参数提供一个值,为什么从路由的默认值中删除它会使我得到的URL有所不同?

Thanks! 谢谢!

Try installing the NuGet package Glimpse . 尝试安装NuGet包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 这是Scott Hanselan关于如何使用它的博客文章: 第5周的NuGet包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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