Question

I have on the controller:

[RoutePrefix("")]

The action with route:

   [Route("things-to-do/{filter1?}/{filter2?}/{filter3?}/{filter4?}/{Area?}", Name = "thingstodo")]
   public async virtual Task<ActionResult> ThingsToDo(string filter1 = "", string filter2 = "", string filter3 = "", string filter4 = "", string q = "")

This works if I enter these url's in browser:

localhost/things-to-do
localhost/things-to-do/something
localhost/things-to-do/something/something

But these all return null:

Url.RouteUrl("thingstodo")  
Url.Action("ThingsToDo", "ControllerName")  
Url.Action(MVC.ControllerName.ThingsToDo())  

These return correct url's:

Url.Action("ThingsToDo", "ControllerName", new { filter1 = "something", filter2 = "something" })
Url.Action(MVC.ControllerName.ThingsToDo("something", "something"))

Any help on how to correctly generate url's when I don't need the optional params would be appreciated.

Was it helpful?

Solution

I found a possible solution:

    [Route("things-to-do/{filter1}/{filter2}/{filter3}/{filter4}/{Area?}", Order = 1)]
    [Route("things-to-do/{filter1}/{filter2}/{filter3}/{Area?}", Order = 2)]
    [Route("things-to-do/{filter1}/{filter2}/{Area?}", Order = 3)]
    [Route("things-to-do/{filter1}/{Area?}", Order = 4)]
    [Route("things-to-do/{Area?}", Order = 5)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top