允许MVC路由中的空参数

时间:2010-12-15 06:07:50

标签: asp.net-mvc routes string

我有一条只能通过自动化流程调用的路线:

routes.MapRoute(
  "Automated file processing",
  "Process/{change}/{file}/{type}",
  new { controller = "C", action = "Process" }
);

filetype都是可选参数。理想情况下,我希望能够致电

/Process/Created/Filename/Text    (with file and type)
/Process/DirectoryListing//Text   (with type only)
/Process/Created/Filename/        (with file only)

你如何在中间实现可选参数?通过我展示的示例路线,即使我将file = "", type = ""添加到路线,我得到:

  

HTTP错误400 - 错误请求。

3 个答案:

答案 0 :(得分:3)

路线中间不能有可选参数。出于显而易见的原因,只有最后一个参数可以是可选的,或者路由引擎无法消除不同可能情况之间的歧义。

答案 1 :(得分:1)

你的答案就在这里:http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

它讨论了问题,并提供了一种解决方法, 希望它有所帮助!

答案 2 :(得分:0)

我相信您需要添加file = UrlParameter.Optional, type = UrlParameter.Optional

对于可选的中间参数,我会在此之前定义另一个路径。

相关问题