gpt4 book ai didi

.net - IgnoreRoute 的简写

转载 作者:行者123 更新时间:2023-12-04 05:41:38 27 4
gpt4 key购买 nike

我注意到一个 SO 答案暗示 or-delimited matches for IgnoreRoute ,像这样:

routes.IgnoreRoute("*.js|css|swf");

当我试一试时,它失败了。我不得不将建议的一行代码转换为多行,如下所示:
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");

实际上是否有更紧凑的方式来表达文件的豁免(例如 css、javascript 等)?另外,我想知道原始链接是否真的有误,或者我只是错过了一些东西。

是的,请假设我想要并且需要 routes.RouteExistingFiles = true

最佳答案

我想出了一个更简单的方法:

routes.RouteExistingFiles = true;
routes.IgnoreRoute("{*relpath}", new { relpath = @"(.*)?\.(css|js|htm|html)" });

也无需担心尾随 http 查询字符串,因为 System.Web.Routing.Route 类在评估期间已经剥离了该部分。

同样有趣的是,Route.GetRouteData(...) 中的代码将采用提供的正则表达式约束,并添加“开始”和“结束”行要求,如下所示:
string str = myRegexStatementFromAbove;
string str2 = string.Concat("^(", str, ")$");

这就是为什么我写的正则表达式如果只是写成就不起作用的原因:
routes.IgnoreRoute("{*relpath}", new { relpath = @"\.(css|js|htm|html)" });

关于.net - IgnoreRoute 的简写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11198535/

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