Wednesday, 1 August 2012

Bundling and Minification in ASP.NET 4.5


Optimizing application performance  is a key element for business. There are several ways by which we can optimize the applications performance. It can be done either by server side code optimization, caching or some client side optimization. In this post I am going to discuss about one handy and smart way to optimize web applications performance using Bundling and Minification  features which is introduced with ASP.NET 4.5 Developer PreviewASP.NET 4.5 Developer Previewintroduced bundling, which combines multiple JavaScript files for faster loading with less number of requests for download and minification, which reduces the size of JavaScript and CSS files by removing unneeded characters .  Combination of these bundling and minification helps web pages to load very faster. Let’s have a looks how it works.
The below images shows the typical web application structure of  that contains CSS and Javascript files along with other asp.net elements.
image
Scripts folder contains all the JavaScript files where as Styles contains all the CSS file. CSS  and JS files takes milliseconds of time  to load into the browser though it’s really matter how much time it’s takes to load the CSS and JS files.
This is how you refer the JavaScript and CSS in applications markup.
image
Run your application and  inspect the loaded Css and JavaScript files using IE Developer toolbar . You can see all the mentioned css and JavaScript in the html markup  loaded individually.
image
To take a more granular look on the loading perspective, you can use the IE Developer toolbar. You will find there are individual request to download the css and javascript files and each of them taken individual time.
image
You can take a quick look using YSlow statistics viewer for total number of request for javascript and css files.
image
ASP.NET 4.5 Introduced Bundling and Minifying the files which reduce the number of requests by merging the files into a single one. Bundling combines multiple JavaScript files for faster loading and reduced the number of request to download the files and minification reduces the size of JavaScript and CSS files by removing unneeded characters.
To apply the binding and  Minifying first of all you need to refer the folder for css and javascript instead of individual files. Along with the folder name you have the append css for CSS folder and js for JavaScript folder.
image
Once you are done with this changes, you have to enable the Bundling on Application_Start() event of global.asax.cs  file
image
That’s all. Run the application once again and inspect the save thing for CSS and JavaScript in IE Developer Toolbar. Interestingly you will find only one CSS File and one JavaScript has been loaded.
image
You can also use IE Developer toolbar to checkout the result. Yes, there is only two request, one for CSS and another for JavaScript. You can also find the significant amount of changes in file size and number of request for JS and CSS file also reduced.
image
image

Here is some inside view,

.NET 4.5 introduced a new class called BundleTable provides programmatic access to the collection of registered Bundleobjects in an ASP.NET application. Bundle object  contains the  list of JavaScript or CSS files . ASP.NET runtime dynamically combines into a single virtual file that a browser can retrieve by using a single request.
image
Every elements of Bundle object is a key value pair . Key is a string that define either “JS” or “Css” and Values contains the the type of  System.Web.Optimization.DynamicFolderBundle.
image
You can create your custom bundles for JavaScript  as well as CSS.  Below code snippets shows the same.
01void Application_Start(object sender, EventArgs e)
02      {
03          BundleTable.Bundles.EnableDefaultBundles();
04
05          var jSBundle = new Bundle("~/JsMinify"typeof(JsMinify));
06
07          jSBundle.AddFile("~/Scripts/CustomFunction.js");
08          jSBundle.AddFile("~/Scripts/jquery-1.4.1-vsdoc.js");
09          jSBundle.AddFile("~/Scripts/jquery-1.4.1.js");
10          jSBundle.AddFile("~/Scripts/JSONCreate.js");
11
12          BundleTable.Bundles.Add(jSBundle);
13
14          var cssBundle = new Bundle("~/CSSMinify"typeof(CssMinify));
15
16          cssBundle.AddFile("~/Styles/Collection.css");
17          cssBundle.AddFile("~/Styles/GlobalSupport.css");
18          cssBundle.AddFile("~/Styles/MasterStyle.css");
19          cssBundle.AddFile("~/Styles/MenuStyle.css");
20          cssBundle.AddFile("~/Styles/Minimum.css");
21          cssBundle.AddFile("~/Styles/Ribbon.css");
22          cssBundle.AddFile("~/Styles/Site.css");
23
24          BundleTable.Bundles.Add(cssBundle);
25
26      }
Once you have your own bundle object, you can specify the same in your html markup as shown in below
Now, run the application and inspect your own created bundle in IE Developer toolbar.
image
One of the biggest advantages of this custom bundle objects is, you can refer multiple directories as shown in below code snippet.image
As shown in above code snippet, we are adding one directory for bundling with filtering criteria  of  “.JS” file. Boolean values indicate while adding the directory, it will ignore the sub directories.
You can find some more information over here ASP.NET 4.5 New Features also a Quick hit video ASP.NET Bundling and Minification

No comments:

Post a Comment