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 に答える
1

ここで私の回答を参照してください: Asp.Net MVC バンドル、不足しているファイルを検出する最良の方法。パスでの 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 に答える