Search code examples
asp.net-mvc-4episerver

Matching route with parameters


I'm new working with routes.

Some one know what this route doesn't work.

/dispatch/Paris/Lixo = page not found

/dispatch?region=Paris&department=Lixo //works.

Global.asax:

        routes.MapContentRoute(name: "customRoute",
                        url: "{region}/{department}",
                        defaults: new { 
    action = "index", 
    controller = "dispatch", 
    region = UrlParameter.Optional, 
    department = UrlParameter.Optional
}
                        //,contentRootResolver: (s) => s.StartPage
                        );

DispatchController.cs

public ActionResult Index(DispatchPage currentPage, string region, string department)

Solution

  • Note that your route includes only region and department. The URL does not assume controller is there. Also, I assume, there is a default route defined. So:

    /dispatch/Paris/Lixo - does not fit anything. {region}/{department} does not have controller, so route processor assigns dispatch to region and Paris to department. Luxo does not fit anything, therefore the whole route does not fit. On the other hand default route assumes Paris is an action, so it does not fit as well.

    /dispatch?region=Paris&department=Lixo - fits default route with action set by default. Notice that query string params do not play any role in routing, except they are passed along, well, as parameters.

    What you are probably after is, I think, this route:

    url: "dispatch/{region}/{department}"
    

    This will capture the first url just fine.