Defining custom URL routes in ASP.Net MVC

人走茶凉 提交于 2019-12-03 09:28:57

问题


I have the following routes defined:

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" });
}

I found some routes to give a 404, so I installed the RouteDebugger NuGet package.

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.

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/5903996/defining-custom-url-routes-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!