2010-01-13 109 views
6

什么是MVC路由约束传递一个有效的正则表达式?例如,我有以下途径:MVC路由约束bool

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

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

通过这种方式,在另一端的动作可以使用布尔类型,如:

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

预先感谢您为您的输入。

回答

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

+2

should not dealerId = @“^ \ d $ +”,be dealerId = @“^ \ d + $”,只需切换最后2个字符 – BlackTigerX 2011-06-17 14:40:45

+0

@BlackTigerX:是的!感谢您指出这一点! – 2011-06-17 18:00:49