0

I am looking for a solution to an MVC 5 problem whereby I would like to use a dynamic subdirectory structure to filter a controller and action

For example if I enter the following url

mysite.com/UK/Essex it calls the controller Home and the action Index but passes the UK and Essex as two parameters.

1 Answers1

0

I found the solution with some playing around

You need to set a new MapRoute BEFORE the default route - see below

public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");

        routes.MapRoute(
            name: "State",
            url: "{country}/{state}",
            defaults: new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            name: "Default", // Route name
            url: "{controller}/{action}/{id}", // URL with parameters
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }