我正在尝试创建一个路线约束,但不确定什么是最好的。这是没有限制的路线:
context.MapRoute(
"Accommodation_accomm_tags",
"accomm/{controller}/{action}/{tag}",
new { action = "Tags", controller = "AccommProperty" },
new { tag = @"" } //Here I would like to put a RegEx for not null match
);
最好的解决方案是什么?
最佳答案
您能否创建一个IRouteConstraint
:
public class NotNullRouteConstraint : IRouteConstraint
{
public bool Match(
HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
return (values[parameterName] != null);
}
}
您可以连接:
context.MapRoute(
"Accommodation_accomm_tags",
"accomm/{controller}/{action}/{tag}",
new { action = "Tags", controller = "AccommProperty" },
new { tag = new NotNullRouteConstraint() }
);
关于ASP.NET MVC 3 路由约束 : Regular Expression for not null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498347/