我有这样的捆绑
bundles.Add(new StyleBundle("~/Content/themes/default/css")
.IncludeDirectory("~/Content/themes/Default", "*.css"));
但我想从中排除一个 CSS 文件。
是否可以在不指定捆绑包中的每个 CSS 文件的情况下做到这一点?
我有这样的捆绑
bundles.Add(new StyleBundle("~/Content/themes/default/css")
.IncludeDirectory("~/Content/themes/Default", "*.css"));
但我想从中排除一个 CSS 文件。
是否可以在不指定捆绑包中的每个 CSS 文件的情况下做到这一点?
尝试使用IgnoreList.Ignore;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"));
我目前不在 PC 上,因此上述内容未经测试,但应该为您提供解决方案的模板。
我已经改进了 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;
}
}
我的一个示例用法是包含 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,这将正常运行。
这是另一个重载现有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());
}
}