bool的MVC路由约束

时间:2010-01-13 18:18:58

标签: asp.net-mvc routes constraints boolean

通过bool的MVC路由约束的有效正则表达式是什么?例如,我有以下路线:

routes.MapRoute("MenuRouteWithExtension",
    "Menu.mvc/{action}/{projectId}/{dealerId}/{isGroup}",
     new { controller = "Menu", action = "RedirectUrl",
           projectId = "", dealerId = "", isGroup = "" }
     new { projectId = @"\d+", dealerId = @"\d+", isGroup = @"???" });

基本上,我需要知道什么是有效的代替???在上面的代码示例中。

这样,另一端的Action可以使用bool类型:

public ActionResult RedirectUrl(int projectId, int dealerId, bool isGroup)

提前感谢您的意见。

1 个答案:

答案 0 :(得分:17)

isGroup = @"^(true|false)$"

因此...

routes.MapRoute(
  "MenuRouteWithExtension",
  "Menu.mvc/{action}/{projectId}/{dealerId}/{isGroup}",
  new
  {
    controller = "Menu",
    action = "RedirectUrl",
    projectId = "",
    dealerId = "",
    isGroup = "" //Possibly set this to 'true' or 'false'?
  },
  new
  {
    projectId = @"^\d+$",
    dealerId = @"^\d+$",
    isGroup = "^(true|false)$"
  }
);

套管无关紧要,因此应接受True以及falSE

此外,我已将^$放在正则表达式值上,以便它们不匹配,例如blahtrueblah