如何解决这两个冲突的 MVC 路由?
这是我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们的目标是消除不明确的匹配,所以让我们来看看如何制定一个更具体的匹配。您的产品路由的控制器是否始终为“产品”?如果是这样,您可以将该路线更改为
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
这种冲突很难解决,因为:
是相同的,并且始终是第一个选择请求的路由。您可能需要设置一些约束(例如,catch-all 值将始终包含破折号,这将使其与其他路由消除歧义,因为操作名称不能包含破折号或修复 catch-all 路由的控制器)。
This conflict is difficult to resolve simply because:
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).