简体   繁体   中英

asp.net scriptbundle multiple include vs single include

What is the difference in bundling:

  bundles.Add(new ScriptBundle("~/bundles/jquery")
  .Include("~/Scripts/jquery-{version}.js","file2.js", "file3.js"));

vs

   bundles.Add(new ScriptBundle("~/bundles/jquery")
                .Include("~/Scripts/jquery-{version}.js")
                .Include("file2.js")
                .Include("file3.js"));

I can put many scripts inside the ONE include method or I can use many include methods.

When should I use what?

Either choice is fine, it is a syntax, readability choice. Include("resource1", "resource2", "resourceN") is simple overload of the Include method that uses the params keyword. In C# the params keyword allows for a variable number of parameters.

Include('Resource1").Include("Resource2").Include("ResourceN") is a different signature of Include method that accepts one string argument. Include("resource1").Include("resource2") is simple chaining.

Either syntax ultimately calls the same code to add the "resource" string paths. You are just calling different signature/overloaded definitions of the Include method to pass your string resouce/js arguments.

There is no difference.

In both ways you create a new ScriptBundle instance named "~/bundles/jquery" that includes all the files in the Scripts folder that match the wild card string "~/Scripts/jquery-{version}.js" and also "file2.js", "file3.js". Then you add the ScriptBundle instance to the BundleCollection instance named bundles using the Add method.

The {version} wild card matching shown above is used to automatically create a jQuery bundle with the appropriate version of jQuery in your Scripts folder. Allows you to use NuGet to update to a newer jQuery version without changing the preceding bundling code or jQuery references in your view pages.


For more info refer to Bundling and Minification .

The choice is based on preference as the first option uses an overloaded method of the Include operation, accepting multiple parameters whereas the second option simply chains the values.

Very similar to using either of two styles of writing LINQ queries... all comes down to your preference.

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