One of the most common techniques for improving website performance is to reduce the number of requests a browser needs to make in order to get the resources required for the page, and to reduce the size of the data requested.
When it comes to both JavaScript and CSS, this generally means combining all of the files of the same type into a single large file (bundling), removing unnecessary whitespace from them, and renaming variables to use the minimum amount of space possible while still leaving the functionality unchanged (minification).
Since version 4.5, ASP.NET supports automatic bundling and minification. By having these processes applied in an automatic way, you can work with the unmodified files to make development easier, while your users get the benefit of the optimized files.
In this recipe, you'll add bundling and minification to a site, and see how it impacts your development activities.
You can use Visual Studio Express 2015 for Web, Visual Studio 2015 Community Edition, or any of the premium editions for this recipe. We are going to use the project created in the Getting started with a Bootstrap-based SPA recipe, but feel free to substitute your own.
This recipe assumes that your browser of choice has developer tools that are able to capture network traffic. If you use Internet Explorer, you will need Internet Explorer 11 or higher, which is what this recipe assumes you are using.
Bundling and minification are important steps to remember when moving your code into production (even if just testing for production-like environments). Let's see how to do it:
http://localhost:37271/Manage
page in your browser, and open the browser's developer tools. If you are using Internet Explorer 11, you can press F12 to open them.BundleConfig.cs
file. ( It is located in the project's App_Start
folder.)Scripts
folder), you will undoubtedly want the bundles file for your own custom JavaScript. To do that, you can define your own bundle by adding a new statement to the RegisterBundles()
method as follows:bundles.Add(new ScriptBundle("~/bundles/customjs"). Include("~/Scripts/myJavaScript.js"));
myJavaScript.js
).Global.asax.cs
file in Solution Explorer. In the Application_Start()
method, add the following highlighted line of code to enable optimizations, as shown in the following image:The EnableOptimizations
property forces bundling to occur. Without this call, bundling only occurs when running the site in the Release mode. In other words, when the debug = true
attribute of the system.web.compilation
tag of the web.config
file is specified, bundling optimizations are disabled.
bootstrap
file from the script's' drop-down list, as shown in the following screenshot:Once the file is open, as you can see in the following screenshot, not only has the whitespace been removed, but the variable names have also been shortened, and the code optimized. Exactly what you should expect minification to do:
Inside the ASP.NET runtime, when a browser requests a page with a bundle in it, ASP.NET will either render the names of the individual files in the bundle or the bundle name itself, depending on whether optimizations are turned on or not. When optimizations are on, browsers will request bundles by their name, and ASP.NET will group the individual bundled files into a single larger file before minifying them and sending the result back to the browser. The resulting minified file is cached by ASP.NET so that future requests do not impact site performance.
You can create your own custom bundle types by subclassing the Bundle
class. This allows you to provide your own minification rules and bundling mechanisms, which is useful if you want to support web technologies such as LESS, Sass, or CoffeeScript.
18.217.8.82