开发者

MVC Route Constraint for bool

开发者 https://www.devze.com 2022-12-16 20:18 出处:网络
What would be a valid regex for a MVC route constraint passing a bool?For example, I have the below route:

What would be a valid regex for a MVC route constraint passing a bool? For example, I have the below route:

routes.MapRoute("MenuRouteWithExtension",
    "Menu.mvc/开发者_如何学Go{action}/{projectId}/{dealerId}/{isGroup}",
     new { controller = "Menu", action = "RedirectUrl",
           projectId = "", dealerId = "", isGroup = "" }
     new { projectId = @"\d+", dealerId = @"\d+", isGroup = @"???" });

Basically, I need to know what would be valid in place of the ??? in the above code example.

This way, the Action on the other end can use the bool type like:

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

Thank you in advance for your input.


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

So...

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)$"
  }
);

Casing shouldn't matter, so True should be accepted, as well as falSE.

Also, I've put ^ and $ on the regex values so that they won't match, for instance blahtrueblah.

0

精彩评论

暂无评论...
验证码 换一张
取 消