我正在使用一些动态捆绑,它根据配置添加 CSS 和 JS 文件。
我启动了一个新的 StyleBundle,这样:
var cssBundle = new StyleBundle("~/bundle/css");
然后遍历配置并添加任何找到的包括:
cssBundle.Include(config.Source);
在循环之后,我想检查是否实际上包含任何文件/目录。我知道有 EnumerateFiles() 但我认为这 100% 不能达到目的。
其他人以前做过类似的事情吗?
我正在使用一些动态捆绑,它根据配置添加 CSS 和 JS 文件。
我启动了一个新的 StyleBundle,这样:
var cssBundle = new StyleBundle("~/bundle/css");
然后遍历配置并添加任何找到的包括:
cssBundle.Include(config.Source);
在循环之后,我想检查是否实际上包含任何文件/目录。我知道有 EnumerateFiles() 但我认为这 100% 不能达到目的。
其他人以前做过类似的事情吗?
该类Bundle使用不向应用程序公开的内部项目列表,并且不一定可以通过反射访问(我尝试过但无法获得任何内容)。您可以使用BundleResolver类获取有关此的一些信息,如下所示:
var cssBundle = new StyleBundle("~/bundle/css");
cssBundle.Include(config.Source);
// if your bundle is already in BundleTable.Bundles list, use that. Otherwise...
var collection = new BundleCollection();
collection.Add(cssBundle)
// get bundle contents
var resolver = new BundleResolver(collection);
List<string> cont = resolver.GetBundleContents("~/bundle/css").ToList();
如果您只需要计数,那么:
int count = resolver.GetBundleContents("~/bundle/css").Count();
编辑:使用反射
显然我之前的反射测试做错了。
这实际上有效:
using System.Reflection;
using System.Web.Optimization;
...
int count = ((ItemRegistry)typeof(Bundle).GetProperty("Items", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(cssBundle, null)).Count;
当然,您可能应该在那里添加一些安全检查,并且像许多反射示例一样,这违反了该属性的预期安全Items性,但它确实有效。
您可以使用以下扩展方法Bundle:
public static class BundleHelper
{
private static Dictionary<Bundle, List<string>> bundleIncludes = new Dictionary<Bundle, List<string>>();
private static Dictionary<Bundle, List<string>> bundleFiles = new Dictionary<Bundle, List<string>>();
private static void EnumerateFiles(Bundle bundle, string virtualPath)
{
if (bundleIncludes.ContainsKey(bundle))
bundleIncludes[bundle].Add(virtualPath);
else
bundleIncludes.Add(bundle, new List<string> { virtualPath });
int i = virtualPath.LastIndexOf('/');
string path = HostingEnvironment.MapPath(virtualPath.Substring(0, i));
if (Directory.Exists(path))
{
string fileName = virtualPath.Substring(i + 1);
IEnumerable<string> fileList;
if (fileName.Contains("{version}"))
{
var re = new Regex(fileName.Replace(".", @"\.").Replace("{version}", @"(\d+(?:\.\d+){1,3})"));
fileName = fileName.Replace("{version}", "*");
fileList = Directory.EnumerateFiles(path, fileName).Where(file => re.IsMatch(file));
}
else // fileName may contain '*'
fileList = Directory.EnumerateFiles(path, fileName);
if (bundleFiles.ContainsKey(bundle))
bundleFiles[bundle].AddRange(fileList);
else
bundleFiles.Add(bundle, fileList.ToList());
}
}
public static Bundle Add(this Bundle bundle, params string[] virtualPaths)
{
foreach (string virtualPath in virtualPaths)
EnumerateFiles(bundle, virtualPath);
return bundle.Include(virtualPaths);
}
public static Bundle Add(this Bundle bundle, string virtualPath, params IItemTransform[] transforms)
{
EnumerateFiles(bundle, virtualPath);
return bundle.Include(virtualPath, transforms);
}
public static IEnumerable<string> EnumerateIncludes(this Bundle bundle)
{
return bundleIncludes[bundle];
}
public static IEnumerable<string> EnumerateFiles(this Bundle bundle)
{
return bundleFiles[bundle];
}
}
然后只需将您的Include()电话替换为Add():
var bundle = new ScriptBundle("~/test")
.Add("~/Scripts/jquery/jquery-{version}.js")
.Add("~/Scripts/lib*")
.Add("~/Scripts/model.js")
);
var includes = bundle.EnumerateIncludes();
var files = bundle.EnumerateFiles();
如果您也在使用IncludeDirectory(),只需通过添加相应的AddDirectory()扩展方法来完成示例。