.net 信号路线速记
我注意到有一个答案是这样的:.net 信号路线速记,.net,asp.net-mvc,.net,Asp.net Mvc,我注意到有一个答案是这样的: routes.IgnoreRoute("*.js|css|swf"); routes.IgnoreRoute("Javascript/{*catchall}"); routes.IgnoreRoute("Content/{*catchall}"); routes.IgnoreRoute("Scripts/{*catchall}"); string str = myRegexStatementFromAbove; string str2 = string.Conc
routes.IgnoreRoute("*.js|css|swf");
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
string str = myRegexStatementFromAbove;
string str2 = string.Concat("^(", str, ")$");
当我尝试时,它失败了。我必须将建议的一行代码转换为多行,如下所示:
routes.IgnoreRoute("*.js|css|swf");
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
string str = myRegexStatementFromAbove;
string str2 = string.Concat("^(", str, ")$");
事实上,是否有一种更简洁的方式来表达对文件的豁免(例如css、javascript等)?另外,我想知道最初的链接是否真的错了,或者我只是错过了什么
是的,请假设我想要并且需要路由。routeeExistingFiles=true我不确定是否可以在一行中指定所有路由。另一种方法是,您可以创建自定义管线约束并完全忽略那些文件夹/文件 更新: 根据@Brent的反馈,检查
pathinfo
比比较文件夹
更好
public class IgnoreConstraint : IRouteConstraint
{
private readonly string[] _ignoreList;
public IgnoreConstraint(params string[] ignoreList)
{
_ignoreList = ignoreList;
}
public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
return _ignoreList.Contains(Path.GetExtension(values["pathinfo"].ToString()));
}
}
Global.asax.cs
routes.IgnoreRoute("{*pathInfo}", new { c =
new IgnoreConstraint(".js", ".css") });
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{folder}/{*pathInfo}", new { c =
new IgnoreConstraint("content", "script") });
routes.RouteExistingFiles = true;
================================================================================
以前的代码
public class IgnoreConstraint: IRouteConstraint
{
private readonly string[] _ignoreArray;
public IgnoreConstraint(params string[] ignoreArray)
{
_ignoreArray = ignoreArray;
}
public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
var folder = values["folder"].ToString().ToLower();
return _ignoreArray.Contains(folder);
}
}
在Global.asax.cs中
routes.IgnoreRoute("{*pathInfo}", new { c =
new IgnoreConstraint(".js", ".css") });
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{folder}/{*pathInfo}", new { c =
new IgnoreConstraint("content", "script") });
routes.RouteExistingFiles = true;
我想出了一个更简单的方法:
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{*relpath}", new { relpath = @"(.*)?\.(css|js|htm|html)" });
也不需要担心跟踪http查询字符串,因为System.Web.Routing.Route类在求值过程中已经剥离了该部分
同样有趣的是,Route.GetRouteData(…)中的代码将接受提供的正则表达式约束,并添加“开始”和“结束”行要求,如下所示:
routes.IgnoreRoute("*.js|css|swf");
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
string str = myRegexStatementFromAbove;
string str2 = string.Concat("^(", str, ")$");
这就是为什么我写的正则表达式如果仅仅写为:
routes.IgnoreRoute("{*relpath}", new { relpath = @"\.(css|js|htm|html)" });
在完成“自定义约束”级别的工作时,我可能最好只使用一个详细的IgnoreRoute语句列表,假设它不是一个很长的列表。不过,如果我测试在{*pathInfo}中找到的文件扩展名,而不是在{folder}段中找到的文件夹,我可能会尝试使用您的解决方案。这样,我就不在乎在哪里找到我的.js、.css或.html文件了。建议自定义约束真是太好了。我同意你检查pathinfo比检查文件夹要好得多。我试着那样做的时候有点懒。嗯,我更新了答案,这样对某人会有帮助。感谢您指出:)