MVC route restriction for bool

What would be the correct regex to restrict the MVC route passing the bool? For example, I have the following route:

routes.MapRoute("MenuRouteWithExtension", "Menu.mvc/{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 true instead ??? in the above code example.

Thus, the Action on the other end can use the bool type, for example:

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

Thank you in advance for your input.

+6
asp.net-mvc boolean constraints routes
source share
1 answer
 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)$" } ); 

The enclosure should not matter, so True must be accepted as well as falSE .

Also, I put ^ and $ in regex values ​​so that they don't match, like blahtrueblah .

+17
source share

All Articles