ASP.NET MVC的路线,不从一些文字 [英] ASP.NET MVC route that doesn't start with some literal
本文介绍了ASP.NET MVC的路线,不从一些文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建一个路由的网址,不从一些立即开始。我创建了以下路由定义:
I need to create a route for url that doesn't start from some literal. I have created the following route definition:
routes.MapRoute("",
"{something}",
new { Controller = "Home", Action = "Index" },
new
{
something = "^(?!sampleliteral)"
});
但看起来像它不工作
but looks like it doesn't work
推荐答案
您可以尝试用一个路由约束:
You may try with a route constraint:
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() }
);
这篇关于ASP.NET MVC的路线,不从一些文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文