问题描述
我有以下途径定义为:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Homepage", url: "", defaults: new { controller = "Restaurants", action = "Search" });
routes.MapRoute(name: "About", url: "about", defaults: new { controller = "Home", action = "About" });
routes.MapRoute(name: "Disclaimer", url: "disclaimer", defaults: new { controller = "Home", action = "Disclaimer" });
routes.MapRoute(name: "Contact", url: "contact", defaults: new { controller = "Home", action = "Contact" });
routes.MapRoute(name: "RestaurantDetails", url: "{id}/{slug}", defaults: new { controller = "Restaurant", action = "Details" });
routes.MapRoute(name: "RestaurantLocationDetails", url: "{id}/{restaurantSlug}/{locationSlug}", defaults: new { controller = "Restaurant", action = "LocationDetails" });
routes.MapRoute(name: "Api", url: "api/{action}", defaults: new { controller = "Api" });
}
我发现了一些路线给予404,所以我安装了RouteDebugger的NuGet包。
I found some routes to give a 404, so I installed the RouteDebugger NuGet package.
这说明我所期望的第4击溃,但在过去的3路我仍然得到404唉路线调试器在页面的底部没有出现 - 我希望它会告诉我哪些位得到映射,但我什么也得不到。存在的所有的意见。
It shows what I expect for the first 4 routs, but on the last 3 routes I still get a 404 and alas Route Debugger doesn't appear at the bottom of the page - I was hoping it would show me which bits got mapped, but I get nothing. All the views exist.
所以我假设我正在做的路由定义一个错误 - 任何人都可以揭示出这个任何光线?另外,我怎么能得到路线调试器给我的网址被如何映射到路线字典那些返回一个404页?
So I'm assuming I'm making a mistake with the route definitions - can anyone shed any light on this? Also, how can I get Route Debugger to show me how the URL gets mapped into the route dictionary for those pages that return a 404?
推荐答案
您需要更改路径的排列顺序。
You need to change the order of the routes.
routes.MapRoute(name: "Homepage", url: "", defaults: new { controller = "Restaurants", action = "Search" });
routes.MapRoute(name: "About", url: "about", defaults: new { controller = "Home", action = "About" });
routes.MapRoute(name: "Disclaimer", url: "disclaimer", defaults: new { controller = "Home", action = "Disclaimer" });
routes.MapRoute(name: "Contact", url: "contact", defaults: new { controller = "Home", action = "Contact" });
routes.MapRoute(name: "Api", url: "api/{action}", defaults: new { controller = "Api" });
routes.MapRoute(name: "RestaurantLocationDetails", url: "{id}/{restaurantSlug}/{locationSlug}", defaults: new { controller = "Restaurant", action = "LocationDetails" });
routes.MapRoute(name: "RestaurantDetails", url: "{id}/{slug}", defaults: new { controller = "Restaurant", action = "Details" });
的路由进行在它们正在添加到路由表的顺序处理。
The routes are processed in the order in which they're added to the route list.
例如: API /动作
也匹配 RestaurantDetails
路线由于在路线网址只有两个参数参数。
For example: api/action
also matches the RestaurantDetails
route since there are only two parameters in the route url parameters.
因此,需要去从特定于一般。一般来说,如果你有两个路由定义相同数量的参数,然后添加的第一条路线将是一个选择。
So it needs to go from specific to general. In general if you have the same number of parameters in two route definitions then the first route added will be the one chosen.
这篇关于定义在ASP.Net MVC自定义URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!