Asp.net mvc asp.net mvc从包中排除css文件

Asp.net mvc asp.net mvc从包中排除css文件,asp.net-mvc,bundling-and-minification,Asp.net Mvc,Bundling And Minification,我有这样的包裹 bundles.Add(new StyleBundle("~/Content/themes/default/css") .IncludeDirectory("~/Content/themes/Default", "*.css")); 但我想从中排除一个CSS文件 是否可以在不指定捆绑包中的每个CSS文件的情况下执行此操作?尝试使用bundles.IgnoreList.Ignore(…)此处可能需要一种扩展方法: public static class Bundle

我有这样的包裹

bundles.Add(new StyleBundle("~/Content/themes/default/css")
       .IncludeDirectory("~/Content/themes/Default", "*.css"));
但我想从中排除一个CSS文件


是否可以在不指定捆绑包中的每个CSS文件的情况下执行此操作?

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

此处可能需要一种扩展方法:

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;
}
然后用法应该是:

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

我现在不在电脑上,所以上面的内容未经测试,但应该为您的解决方案提供一个模板。

这里有另一个扩展方法,它重载现有的
IncludeDirectory
方法并支持搜索子目录

public static class BundleExtensions
{
    public static Bundle IncludeDirectory(
        this Bundle bundle,
        string directoryVirtualPath,
        string searchPattern,
        params string[] filesToExclude)
    {
        return IncludeDirectory(bundle, directoryVirtualPath, searchPattern, false, filesToExclude);
    }
    public static Bundle IncludeDirectory(
        this Bundle bundle,
        string directoryVirtualPath,
        string searchPattern,
        bool searchSubdirectories,
        params string[] filesToExclude)
    {
        var physicalPath = HttpContext.Current.Server.MapPath(directoryVirtualPath);
        return bundle.Include(Directory
            .EnumerateFiles(physicalPath, searchPattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
            .Select(physicalFileName => physicalFileName.Replace(physicalPath, directoryVirtualPath).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))
            .Where(virtualFileName => !filesToExclude.Contains(virtualFileName))
            .ToArray());
    }
}

我改进了Jon Malcolm的好建议(以及Satpal的一些更新),以修复它的一些缺点:

1) 它使用“.min.”文件打破捆绑包的默认行为

2) 它不允许搜索模式,只允许排除文件

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Optimization;


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

        SearchOption searchOption = includeSubDirectories
                                        ? SearchOption.AllDirectories
                                        : SearchOption.TopDirectoryOnly;

        HashSet<string> excludedFiles = GetFilesToExclude(folderPath, searchOption, excludePatterns);
        IEnumerable<string> resultFiles = Directory.GetFiles(folderPath, searchPattern, searchOption)
                                                    .Where(file => !excludedFiles.Contains(file) && !file.Contains(".min."));

        foreach (string resultFile in resultFiles)
        {
            bundle.Include(directoryVirtualPath + resultFile.Replace(folderPath, "")
                    .Replace("\\", "/"));
        }

        return bundle;
    }

    private static HashSet<string> GetFilesToExclude(string path, SearchOption searchOptions, params string[] excludePatterns)
    {
        var result = new HashSet<string>();

        foreach (string pattern in excludePatterns)
        {
            result.UnionWith(Directory.GetFiles(path, pattern, searchOptions));
        }

        return result;
    }
}

如果您同时拥有angular.min.js和angular.js,并在调试中添加未统一的版本,并在发行版中使用现有的.min.js,那么这将正常运行。

为什么不将css文件移出该目录?当然,我可以这样做,并为此文件指定另一个捆绑包,但是最好将所有文件放在一个目录中,如果我不需要它,我会将它从捆绑包中排除。你有很多文件吗?如果你有少量的文件,你可以单独注册每个css。我有大约20个文件,但例如,如果我向其中添加新文件,我需要在bundle中注册此文件。。。通过这种方式,我可以排除不必要的问题,也不用担心名称。我扩展了这个答案,添加了搜索模式,并修复了wrt to.min版本的缺点。谢谢!这个答案也解决了我在捆绑销售中遇到的一个问题。它没有解释如何使用Ignore或如何访问它。这只是一个指向一些糟糕文档的链接,其中有一个小片段,不能解释这里的任何特性。关于wordpress网站的评论是这里最有用的东西。
bundles.Add(new Bundle("~/bundles/scripts")
                .Include("~/wwwroot/lib/angular/angular.js") // Has to be first
                .IncludeDirectoryWithExclusion("~/wwwroot/lib", "*.js", true, "*.locale.*.js"));