asp.net mvc从bundle中排除css文件

xur*_*rca 25 asp.net-mvc bundling-and-minification

我有这样的捆绑

bundles.Add(new StyleBundle("~/Content/themes/default/css")
       .IncludeDirectory("~/Content/themes/Default", "*.css"));
Run Code Online (Sandbox Code Playgroud)

但我想从中排除一个css文件.是否可以在不指定bundle中的每个css文件的情况下进行此操作?

Cof*_*ode 37

尝试使用IgnoreList.Ignore ; bundles.IgnoreList.Ignore(...).

  • 有一个很好的例子,请参阅:https://stack247.wordpress.com/2014/04/18/asp-net-mvc-4-bundles-ignore-list/ (5认同)
  • 这是一个糟糕的答案.它没有解释如何使用Ignore或如何访问它.它只是一个链接到一些糟糕的文档与一个小片段,解释这里没有任何特质.关于wordpress网站的评论是最有用的. (2认同)

小智 13

您可以在此处使用扩展方法:

public static class BundleExtentions
{
    public static Bundle IncludeDirectoryWithExclusion(this StyleBundle bundle, string directoryVirtualPath, string searchPattern, params string[] toExclude)
    {
        var folderPath = HttpContext.Current.Server.MapPath(directoryVirtualPath);

        foreach (var file in Directory.GetFiles(folderPath, searchPattern))
        {
            if (!String.IsNullOrEmpty(Array.Find(toExclude, s => s.ToLower() == file.ToLower())))
            {
                continue;
            }     

            bundle.IncludeFile(directoryVirtualPath + "/" + file);
        }

        return bundle;
}
Run Code Online (Sandbox Code Playgroud)

然后使用应该是:

bundles.Add(new StyleBundle("~/Content/themes/default/css")
   .IncludeDirectoryWithExclusion("~/Content/themes/Default", "*.css", "file-you-dont-want.css"));
Run Code Online (Sandbox Code Playgroud)

我目前不在PC上,因此上述内容未经测试,但应该为您提供解决方案的模板.