Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining custom URL routes in ASP.Net MVC

Tags:

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?

like image 242
Saqib Avatar asked May 05 '11 20:05

Saqib


1 Answers

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.

like image 140
Buildstarted Avatar answered Dec 06 '22 16:12

Buildstarted