gpt4 book ai didi

c# - 不以某些文字开头的 ASP.NET MVC 路由

转载 作者:太空狗 更新时间:2023-10-30 01:26:39 24 4
gpt4 key购买 nike

我需要为不是从某些文字开始的 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/

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