ASP.Net MVC RC1 RouteCollection.MapRoute 问题

发布于 2024-07-13 21:29:05 字数 961 浏览 8 评论 0原文

我对 ASP.Net MVC 的 RC1 版本有疑问。 每当我在“默认”路由之前添加路由时,创建的结果 URL 都是针对添加的第一个路由。

这是我在 Global.asax.cs 中的路由,

routes.MapRoute(
            "product-detailed",
            "Products/{controller}/{action}/{id}",
            new { controller = "ProductSubType", action = "Index", id = "" }
        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

我的 Url 创建:

        <%= Html.ActionLink("Bikes", "Index", "Bikes") %><br />
        <%= Html.RouteLink("Bikes", "product-detailed", new { controller = "Bikes", action = "Index" }) %>

我希望第一个 ActionLink 创建一个像“/Bikes/Index”这样的 Url,第二个 RouteLink 创建“/Products/Bikes/Index”,但是两个 URL 都结束为“/产品/自行车/索引”。

我在路由中缺少什么?

谢谢。

I have a problem with the RC1 version of ASP.Net MVC. Whenever I add a Route before the "Default" route, the resulting Urls created are for the first Route added.

Here is my Routing in Global.asax.cs

routes.MapRoute(
            "product-detailed",
            "Products/{controller}/{action}/{id}",
            new { controller = "ProductSubType", action = "Index", id = "" }
        );

        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

My Url creation:

        <%= Html.ActionLink("Bikes", "Index", "Bikes") %><br />
        <%= Html.RouteLink("Bikes", "product-detailed", new { controller = "Bikes", action = "Index" }) %>

I would expect the first ActionLink to create a Url like "/Bikes/Index" and the second RouteLink to create "/Products/Bikes/Index", but both Urls end up as "/Products/Bikes/Index".

What am I missing here on the routing?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女猫性小仙女 2024-07-20 21:29:05

你没有遗漏任何东西。 它按设计工作。

由于控制器和操作在顶部路由中都是变量,并且对有效值没有限制,因此该路由对于控制器和操作的所有值都有效。

潜在的解决方法:

  • 修复控制器和/或操作值,使它们不再是 URL 的一部分
  • 为控制器和/或操作值的顶级路由添加限制
  • 始终使用路由链接而不是操作链接,因为它们明确指出哪条路线是正确的路线。

You're not missing anything. It's working as designed.

Since the controller and action are both variable in the top route, with no limitations on valid values, then that route is valid for all values of controller and action.

Potential work-arounds:

  • Fix the controller and/or action values so that they're not part of the URL
  • Add restrictions for the top route for values of controller and/or action
  • Always use route links instead of action links, since they unambiguously state which route is the correct route.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文