Bạn có thể sử dụng các phương pháp khuyến nông sau cho 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];
}
}
Sau đó chỉ cần thay thế Include()
cuộc gọi của bạn với 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();
Nếu bạn cũng đang sử dụng IncludeDirectory()
, chỉ cần hoàn thành ví dụ bằng cách thêm phương thức mở rộng AddDirectory()
tương ứng.
Nguồn
2014-09-12 11:16:09
Cảm ơn Corey có vẻ tốt, chỉ có vấn đề tôi có là gói được thêm vào bộ sưu tập để nội dung của nó được tính. Lý tưởng nhất là tôi muốn kiểm tra nội dung để đảm bảo có> 0 TRƯỚC KHI thêm vào bộ sưu tập. Điều đó có ý nghĩa? – timothyclifford
Bạn có thể sử dụng ví dụ phản chiếu để lấy số đếm, hoặc tạo một bộ sưu tập mới (và tạm thời) như tôi đã làm trong mẫu mã ban đầu ở trên và sử dụng 'BundleResolver' để lấy danh sách các tệp được bao gồm. Gói không có sẵn cho phần còn lại của hệ thống cho đến khi bạn thêm nó vào bộ sưu tập 'BundleTable.Bundles', vì vậy bạn có thể sử dụng một trong hai phương pháp để kiểm tra gói trước khi làm cho nó luôn sẵn sàng. – Corey
Không phải là ItemRegistry nội bộ? – timothyclifford