我需要为不是从某些文字开始的 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() }
);
关于c# - 不以某些文字开头的 ASP.NET MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4415890/