19

如果缺少 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);
                }
            }
        }
    }
}   
4

5 回答 5

6

我使用这个类而不是ScriptBundle

public class ScriptBundleWithException : ScriptBundle
{
    public ScriptBundleWithException( string virtualPath ) : base( virtualPath ) {}
    public ScriptBundleWithException( string virtualPath, string cdnPath ) : base( virtualPath, cdnPath ) {}

    public override Bundle Include( params string[] virtualPaths )
    {
        foreach ( var virtualPath in virtualPaths ) {
            Include( virtualPath );
        }
        return this;
    }

    public override Bundle Include( string virtualPath, params IItemTransform[] transforms )
    {
        var realPath = System.Web.Hosting.HostingEnvironment.MapPath( virtualPath );
        if ( !File.Exists( realPath ) ) {
            throw new FileNotFoundException( "Virtual path not found: " + virtualPath );
        }
        return base.Include( virtualPath, transforms );
    }
}

然后在配置中:

bundles.Add( new ScriptBundleWithException( "~/bundles/something" ) //...
于 2014-07-17T19:45:34.897 回答
2

如果您查看 Bundle 类的源代码,您会发现 .Include 只是默默地不添加文件,如果它不存在。因此,您的断言不起作用。

我最终使用了一组脚本文件名并在添加到捆绑包之前手动检查它们。

于 2013-11-22T09:14:33.890 回答
2

我遇到了这个,并认为我会提交我的解决方案。正如其他人提到的那样,将不存在的文件添加到捆绑包中会被忽略。

示例代码:

string[] jsFiles = { "~/js/file1.js", "~/js/file2.js" };
string[] cssFiles = { "~/css/file1.css", "~/css/file2.css" };
List<string> allFiles = new List<string>();
allFiles.AddRange(jsFiles);
allFiles.AddRange(cssFiles);
var server = HttpContext.Current.Server;
foreach(var file in allFiles) {
    if(!File.Exists(server.MapPath(file))
        throw new FileNotFoundException("File " + file + " was not found.");
}

然后将您的捆绑包定义为正常,使用数组.Include

于 2014-02-18T02:01:32.297 回答
1

请在此处查看我的答案:Asp.Net MVC Bundling, best way to detect missing file。它支持 ASP.NET 在路径中的通配符。

于 2014-09-11T10:13:05.697 回答
0

我使用T4MVC

var bundleJsLayout = new ScriptBundle("~/bundle/js").Include(
    "~" + Links.Scripts.jquery_1_9_0_js,
    "~" + Links.Scripts.modernizr_2_6_2_js,
    "~" + Links.Scripts.jquery_unobtrusive_ajax_min_js
    );

bundleJsLayout.Transforms.Add(new JsMinify());
bundles.Add(bundleJsLayout);
于 2013-01-26T00:00:42.043 回答