本文介绍了需要了解为什么ASP.NET MVC Routing在这种情况下会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试添加路由模式以及defult路由规则。 routes.MapRoute( name: myRouting, url: {action} / {controller} / {id},默认值: new {action = 索引,controller = Home,id = UrlParameter.Optional} ); public static void RegisterRoutes(RouteCollection路由) { routes.IgnoreRoute( {resource}。 AXD / {*} PATHINFO); routes.MapRoute( name: myRouting, url: {action} / {controller} / {id},默认值: new {action = 索引,controller = Home,id = UrlParameter.Optional} ); routes.MapRoute( name: 默认, url: {controller} / {action} / {id},默认值: new {controller = Home,action = 索引,id = UrlParameter.Optional} ); } 通过这些路由, http:// localhost:29535 / Edit /书--->工作 http:// localhost:29535 /预订/编辑---> 错误(无法找到资源)此处,Book 是控制器,编辑 行动 但我期待两者都能奏效。 我在这里理解的基本上是缺少的。请帮助我理解这背后的规则。 注意:如果我在我的网址中进行更改(在myRouting中)为 url: {action} / 2 / {controller} / {id} , http:// localhost:29535 / Edit / 2 / Book --->工作 http:// localhost:29535 /预订/编辑--->工作 谢谢, Prabhu 解决方案 问题是您的路由签名完全相同,并且您的路由表无法区分这两者。在这个例子中评估的第一个将是使用的路线。 这里最简单的方法是修改新路线的签名: routes.MapRoute( name: myRouting, url: myroute / {action} / {controller } / {id},默认值: new {action = 索引,controller = 主页 ,id = UrlParameter.Optional} ); 并将myroute /附加到使用该模式的链接。 Hi, I'm trying to add my routing pattern along with the defult routing rules. routes.MapRoute( name: "myRouting", url: "{action}/{controller}/{id}", defaults: new {action = "Index", controller = "Home", id = UrlParameter.Optional } );public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "myRouting", url: "{action}/{controller}/{id}", defaults: new {action = "Index", controller = "Home", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }With these routing,http://localhost:29535/Edit/Book ---> Workinghttp://localhost:29535/Book/Edit ---> Error (The resource cannot be found)Here, Book is Controller, Edit is ActionBut I was expecting both should work.What I'm basically missing to understand here. Please help me understand the rules behind this.Note: If I make changes in my url (in myRouting) as url: "{action}/2/{controller}/{id}", http://localhost:29535/Edit/2/Book ---> Workinghttp://localhost:29535/Book/Edit ---> WorkingThanks,Prabhu 解决方案 The issue is that your route signatures are precisely the same, and there is no way for your Route table to differentiate between the two. The first one evaluated in this instance will be the route that is used.The easiest trick here is to modify the signature of your new route:routes.MapRoute( name: "myRouting", url: "myroute/{action}/{controller}/{id}", defaults: new {action = "Index", controller = "Home", id = UrlParameter.Optional } );And append "myroute/" to your links that use that pattern. 这篇关于需要了解为什么ASP.NET MVC Routing在这种情况下会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 11:18