Answers to my questions:
For the first question, I created my own IsOptionalOrMatchesRegEx constraint:
public class IsOptionalOrMatchesRegEx : IRouteConstraint { private readonly string _regEx; public IsOptionalOrMatchesRegEx(string regEx) { _regEx = regEx; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { var valueToCompare = values[parameterName].ToString(); if (string.IsNullOrEmpty(valueToCompare)) return true; return Regex.IsMatch(valueToCompare, _regEx); } }
Then routes A and B can be expressed in one route:
- url: "nl / blog / {articlepage}"
- defaultvalues: new {articlepage = UrlParameter.Optional}
- restrictions: new {articlepage = new IsOptionalOrMatchesRegEx (@ "\ d")
For question 2, I created an ExcludeConstraint:
public class ExcludeConstraint : IRouteConstraint { private readonly List<string> _excludedList; public ExcludeConstraint(List<string> excludedList) { _excludedList = excludedList; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { var valueToCompare = (string)values[parameterName]; return !_excludedList.Contains(valueToCompare); } }
Route D could be changed as follows:
- url: "nl / blog / {urltitle}"
- constraints: new {urltitle = new ExcludeConstraint (new list () {"addarticle", "addcomment", "gettags"})}));
source share