简体   繁体   中英

What kind of file we can use in asp.net bundling

I want to know is it possinle to add ascx file in bundling like

  bundles.Add(new ScriptBundle("~/bundles/First").Include(
                    "~/Script/js.ascx"));

I want to know because suppose I have to add one more file in bundling after product release then I have to build again for that.

No we cannot include ascx file in bundles We use only js and css files for bundling...here is a small explanation why we use bundles

As a Web application becomes more complex, the number of external scripts on which it depends grows significantly. Start with jQuery and some application libraries like Backbone.js (and its dependency, Underscore) , then throw in plugins from UI frameworks like Foundation and some form validation scripts—not to mention your app's code itself!—and you can easily discover your app has dozens of external files, each of which must be loaded on every page refresh.

To combat this problem, developers often use tools called “bundlers” , which concatenate script files—or stylesheets—together into a single file, thereby reducing the I/O overhead of sending multiple files across the Internet. Many also perform a process known as “minification”, whereby non-essential whitespace is removed and variable names are changed to be as short as possible, resulting in an unreadable—but highly efficient—piece of code.

ASP MVC provides two tailored classes for bundling : ScriptBundle , for JavaScript files, and StyleBundle , for CSS. Both of these classes provide built-in minification, thus reducing your total payload even further. You'll need to specify a unique path for each bundle.

var myBundle = new ScriptBundle("~/bundles/js");

Using this bundle, you can simply add new files (or entire directories!) by using its methods:

myBundle.IncludeDirectory("~/js/vendor/");
myBundle.Include("~/js/app.js");

Once you're done with each bundle, add it to the collection passed in as a parameter

bundles.Add(myBundle);

Continue this pattern until you've configured all of the bundles you'd like.

Well I can't answer the specific question (haven't tried), but to tackle the "issue":

I want to know because suppose I have to add one more file in bundling after product release then I have to build again for that.

As per the docs on bundling , you have options to include files and even directories..and wildcards:

So:

BundleCollection bundles = BundleTable.Bundles;

bundles.Add((new ScriptBundle("~/bundle/custom")
     .IncludeDirectory("~/Scripts/edsf/", "ed*")));

Means:

  • I'm targeting a folder /Scripts/edsf
  • I want all the scripts starting with "ed" , in the stated directory, to be in the script bundle, eg
    • edsf.js
    • edfoo.js

If I add a new file, edbar.js , to the /Scripts/edsf directory, it will be bundled automatically, and you'll see the version string get updated.

No need to build....

Hth...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM