如果缺少 javascript 或 css 文件,是否有可能引发异常?
我已经尝试过使用下面的代码,但它永远不会引发异常......
public class BundleConfig {
public static void RegisterBundles(BundleCollection bundles) {
....
bundles.Add(new ScriptBundle("~/bundles/something").Include("~/Scripts/nonExistingFile.js"));
// I would like the above usage of Include method with
// a non-existing file to throw an exception to make it
// obvious that an expected file is missing ...
// but since it does not I tried to implement a method as below ...
...
AssertThatAllFilesExist(bundles);
// but unfortunately an exception is never thrown but when
// iterating the files in the method below,
// the non-existing file seems to never become retrieved ...
}
private static void AssertThatAllFilesExist(BundleCollection bundles) {
HttpContext currentHttpContext = HttpContext.Current;
var httpContext = new HttpContextWrapper(currentHttpContext);
foreach (var bundle in bundles) {
var bundleContext = new BundleContext(httpContext, bundles, bundle.Path);
IEnumerable<FileInfo> files = bundle.EnumerateFiles(bundleContext);
foreach (var file in files) {
if (!file.Exists) {
// if this problem would occur then it should
// indicate that one of the arguments of the
// method "Bundle.Include" does not
// correspond to an existing file ...
throw new ArgumentException("The following file does not exist: " + file.FullName);
}
}
}
}
}