答案我自己的問題:
對於問題一,我創建了一個自定義的約束IsOptionalOrMatchesRegEx:
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);
}
}
然後,點A和B可以在一個路線中過表達:
- 網址: 「COM /博客/文章{PAGE}」
- 默認值:新的文章頁面= {UrlParameter。可選}
- 約束:新的{articlepage =新IsOptionalOrMatchesRegEx(@ 「\ d」)
對於問題2,I創建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);
}
}
然後
路線d可以被改變,如:
- URL: 「NL /博客/ {urltitle}」
- 約束:新的{urltitle =新ExcludeConstraint(新列表(){「一ddarticle「,」addcomment「,」gettags「})}));
不錯的問題。我對任何答案都很感興趣。 – Phil
也許自定義路由約束可能是最優雅的解決方案? –
好的,對於問題2我自己找到了答案。我創建了一個名爲ExcludeConstraint的自定義約束,並將路由D更改爲:route「nl/blog/{urltitle}」,約束爲「new {urltitle = new ExcludeConstraint(new List(){」addarticle「,」addcomment「,」gettags 「})}));」。現在,我的網址保持乾淨,如/ nl/blog/addcomment –