0

我在控制器上有:

[路由前缀(“”)]

带有路由的动作:

   [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 = "")

如果我在浏览器中输入这些网址,这将有效:

本地主机/待办事项
本地主机/待办事项/某事
本地主机/待办事项/某事/某事

但是这些都返回null:

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

这些返回正确的网址:

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

当我不需要可选参数时如何正确生成 url 的任何帮助将不胜感激。

4

1 回答 1

3

我找到了一个可能的解决方案:

    [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)]
于 2014-03-13T12:00:09.973 回答