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

时间:2012-12-21 12:55:20

标签: asp.net-mvc bundling-and-minification

我有这样的捆绑

bundles.Add(new StyleBundle("~/Content/themes/default/css")
       .IncludeDirectory("~/Content/themes/Default", "*.css"));

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

4 个答案:

答案 0 :(得分:36)

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

答案 1 :(得分: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;
}

然后用法应该是:

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

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

答案 2 :(得分:4)

我已经改进了Jon Malcolm的好建议(以及Satpal here的一些更新),以解决它有的一些缺点:

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;
    }
}

我的一个示例用法是包含lib文件夹中以angular开头的所有库,但不包括所有语言环境脚本(因为稍后将根据语言在另一个包中添加一个):

bundles.Add(new Bundle("~/bundles/scripts")
                .Include("~/wwwroot/lib/angular/angular.js") // Has to be first
                .IncludeDirectoryWithExclusion("~/wwwroot/lib", "*.js", true, "*.locale.*.js"));

如果你同时拥有angular.min.js和angular.js并在调试中添加未分化的版本并在发行版中使用现有的.min.js,这将表现正常。

答案 3 :(得分:1)

这是另一种扩展方法,它重载现有的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()); } } 方法并支持搜索子目录。

<a download href="uploads/file_name.ext"> Download </a>