我有URL的意思是:
items)
为此,我已确定了路线:
问题1:这很好,但也许有一个更好的解决方案,以减少路线?
问题2:为了添加一篇新文章,我在BlogController中有一个操作方法BlogController。当然,有了上面定义的路线,url“/nl/blog/ above”就会映射到路由D,在那里添加文章将是不正确的urltitle。因此,我添加了以下路由:
路由"nl/blog/_{action}":
因此,现在url“/nl/blog/_ action”映射到这个路由,并执行正确的操作方法。但我想知道是否有更好的方法来处理这件事?
谢谢你的建议。
发布于 2011-10-24 12:22:36
对我自己问题的回答:
对于第一个问题,我创建了一个自定义约束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可以用一条路线表示:
"nl/blog/{articlepage}"
对于问题2,我创建了一个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如下:
"nl/blog/{urltitle}"
https://stackoverflow.com/questions/7873091
复制相似问题