MVC Routes with Regular expression

僤鯓⒐⒋嵵緔 提交于 2019-12-12 04:08:50

问题


I'm trying to put a constraint onto a route that does not want to work. Instead to provide a constraint with a list of controllers to allow, I am trying to restrict this route to all controllers except ProjectController.

    context.MapRoute("Project_Projects",
        "Project/{prj}/{controller}/{action}/{id}",
        new { controller = "Dashboard", action = "Index", 
              id = UrlParameter.Optional },
        new
        {
            prj = new ProjectRouteConstraint(),
            controller = @"[^Project]"
        }
        );

This route should be used for all controllers except for the ProjectController. As far as the documentation go it will uses Regex.IsMatch function to check it, and I've double check the regular expression and seems correct, and but it does not seem to work.

Am I missing something?


回答1:


Try the following constraint:

controller = @"^(?!project$).*$"


来源:https://stackoverflow.com/questions/9767625/mvc-routes-with-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!