ASP.NET MVC4 Bundle a single file

前端 未结 3 1296
南方客
南方客 2021-02-14 18:57

Is there a way to bundle a single file using the new bundling features in MVC4? I know bundling a single file doesn\'t make much sense but I\'d like to use server-side minificat

3条回答
  •  心在旅途
    2021-02-14 19:30

    I dont know, if I get you right, but you can add bundles any time you want to, just add it to the bundlecollection:

    BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/myBundleName").Include(
                "~/Scripts/myscript.js"
                ));
    

    I am not sure how you planing to include the bundle on client side, but after a bundle is registered you can get the url of the minified script file with this code:

    string url = BundleTable.Bundles.ResolveBundleUrl("~/Scripts/myBundleName");
    

    In my case I just get this url through an ajax request and create a script tag on client side. This way the script file will be minified and the url comes with an hash code, so the browser should cache it and when the bundle is changed should load the newer version.

    I hope this is, what you intended to do.

提交回复
热议问题