如何解决这两个冲突的 MVC 路由?

发布于 2024-10-15 06:05:19 字数 514 浏览 4 评论 0原文

这是我尝试使用 MVC3 映射的 URL

routes.MapRoute( "Products", "{controller}/{id}/{*name}", new { action = "view" }, new { id = @"\d+" } );

/products/13/seo 友好的产品名称

现在我需要映射的下一条路线是这样的

routes.MapRoute( "General", "{controller}/{id}/{action}", new { }, new { id = @"\d+" } );

/user/42/changepassword

我想知道如何解决这个问题。仅仅更改顺序是不够的,因为应用程序的一个区域停止工作。我知道 {*name} 和 {action} 存在冲突,但我不知道如何解决此问题。

Here are the URL's that I'm trying to map with MVC3

routes.MapRoute( "Products", "{controller}/{id}/{*name}", new { action = "view" }, new { id = @"\d+" } );

/products/13/seo-friendly-name-of-the-product

Now the next route I need to map is this

routes.MapRoute( "General", "{controller}/{id}/{action}", new { }, new { id = @"\d+" } );

/user/42/changepassword

I want to know how to resolve this problem. Simply changing the order isn't enough because one area of the app stops working. I know that {*name} and {action} are being conflicted, but I don't know what to do to fix this.

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

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

发布评论

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

评论(2

只是在用心讲痛 2024-10-22 06:05:19

我们的目标是消除不明确的匹配,所以让我们来看看如何制定一个更具体的匹配。您的产品路由的控制器是否始终为“产品”?如果是这样,您可以将该路线更改为

routes.MapRoute(
    "Products", // Route name
    "products/{id}/{*name}", // URL with parameters
    new { action = "view", controller="Products" }, // defaults
    new { id = @"\d+" } // constraints
);

The goal is to eliminate ambiguous matches, so let's look at making one more specific. Is the controller for your Products route always "Products"? If so, could you change that route to

routes.MapRoute(
    "Products", // Route name
    "products/{id}/{*name}", // URL with parameters
    new { action = "view", controller="Products" }, // defaults
    new { id = @"\d+" } // constraints
);
段念尘 2024-10-22 06:05:19

这种冲突很难解决,因为:

/products/13/seo-friendly-name-of-the-product
/user/42/changepassword

是相同的,并且始终是第一个选择请求的路由。您可能需要设置一些约束(例如,catch-all 值将始终包含破折号,这将使其与其他路由消除歧义,因为操作名称不能包含破折号或修复 catch-all 路由的控制器)。

This conflict is difficult to resolve simply because:

/products/13/seo-friendly-name-of-the-product
/user/42/changepassword

are the same and it will always be the first route that will pick the request. You might need to put some constraints (like for example saying that the catch-all value will always contain dashes which would disambiguate it from the other route because an action name cannot contain dashes or fix the controller for the catch-all route).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文