gpt4 book ai didi

ASP.NET MVC 3 路由约束 : Regular Expression for not null

转载 作者:行者123 更新时间:2023-12-02 14:35:19 24 4
gpt4 key购买 nike

我正在尝试创建一个路线约束,但不确定什么是最好的。这是没有限制的路线:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com