ASP.NET MVC路由不以某些文字开头
我需要为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() } );