我需要为 url 创建一条不从某些文字开始的路由。我创建了以下路由定义:
routes.MapRoute("",
"{something}",
new { Controller = "Home", Action = "Index" },
new
{
something = "^(?!sampleliteral)"
});
但看起来不起作用
您可以尝试使用路线限制:
public class MyConstraint: IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var value = values[parameterName] as string;
if (!string.IsNullOrEmpty(value))
{
return !value.StartsWith("sampleliteral", StringComparison.OrdinalIgnoreCase);
}
return true;
}
}
进而:
routes.MapRoute(
"",
"{something}",
new { Controller = "Home", Action = "Index", something = UrlParameter.Optional },
new { something = new MyConstraint() }
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)