आप 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()
के साथ बदलें: आप भी IncludeDirectory()
उपयोग कर रहे हैं, बस पूरा
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();
एक संबंधित AddDirectory()
विस्तार विधि जोड़कर उदाहरण।
स्रोत
2014-09-12 11:16:09
धन्यवाद कोरी जो अच्छा लग रहा है, केवल मेरे पास यह मुद्दा है कि संग्रह की गणना के लिए संग्रह में बंडल जोड़ा जा रहा है। आदर्श रूप से मैं संग्रह में जोड़ने से पहले> 0 सुनिश्चित करने के लिए सामग्री जांचना चाहता हूं। क्या इसका कोई मतलब है? – timothyclifford
आप गिनती प्राप्त करने के लिए प्रतिबिंब उदाहरण का उपयोग कर सकते हैं, या एक नया (और अस्थायी) संग्रह बना सकते हैं जैसा कि मैंने उपरोक्त प्रारंभिक कोड नमूने में किया था और शामिल फ़ाइलों की सूची प्राप्त करने के लिए 'बंडलरोलॉल्वर'' का उपयोग किया था। जब तक आप इसे 'BundleTable.Bundles' संग्रह में नहीं जोड़ते हैं, तब तक बंडल शेष सिस्टम तक उपलब्ध नहीं होता है, ताकि आप इसे आम तौर पर उपलब्ध कराने से पहले बंडल की जांच करने के लिए किसी भी विधि का उपयोग कर सकें। – Corey
आइटम रजिस्ट्री आंतरिक नहीं है हालांकि? – timothyclifford