Dot Net Stuff

ASP.NET MVC 4 Bundling and Minification - Performance Optimization


Bundling and minification of CSS and JavaScript is one of the most popluar and powerful feature of ASP.NET MVC. Bundling and minification reduces the number of HTTP Requests and payload size resulting in faster and better performing ASP.NET MVC Websites. There are a number of ways you can reduce and combine the size of CSS and JavaScript using the new feature, and in this ASP.NET MVC 4 Tutorial We will learn how you can create custom bundles where you specify the name and order of CSS and JavaScript Files. This method will work compatable with ASP.NET MVC 5 or lower version. While Bundling and minification of CSS and JavaScript in ASP.NET vNext can be achieved gruntjs. If you are looking for Bundling and minification of CSS and JavaScript in ASP.NET MVC 6, click here to read the article.

ASP.NET MVC 4 BundleTable and Bundles

Let try to understand by creating an empty ASP.NET MVC 4 Project, When you create a new empty MVC 4 project, you will notice a couple of changes in the _Layout.cshtml and Global.asax.cs files. The _Layout.cshtml file will include Razor Code referencing System.Web.Optimization and BundleTable.Bundles in the CSS and JavaScript References in the head of the file

 
@Styles.Render("~/Content/css") 
        @Scripts.Render("~/bundles/modernizr") 
. 
. 
. 
. 
@Scripts.Render("~/bundles/jquery") 
        @RenderSection("scripts", required: false)

These references are for ASP.NET MVC 4 and will be shortened in the production version of ASP.NET MVC 4 as a simple HTML Helper. If you look at the actual source code in the browser, you will see these get rendered as:

<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
... 
.. 
.. 
<script src="/Scripts/jquery-1.8.2.js"></script>

The CSS and JavaScript files in the empty ASP.NET MVC 4 project end up being minified and compressed. The result is less HTTP Requests in your ASP.NET MVC 4 web application and a reduction in the size of the CSS and JavaScript sent by the IIS Web Server. In Global.asax.cs you will notice a reference to the new BundleTable in Application Startup:

 
 BundleConfig.RegisterBundles(BundleTable.Bundles);

This is a method in System.Web.Optimization that registers two bundles that combine and minify the default CSS and JavaScript that comes with the "empty" ASP.NET MVC 4 Project.

ASP.NET MVC 4 Custom css and js Bundling & Minification

In BundleConfig.cs class file in your App_Start folder, you will find following code which will use to add our custom css & js file. Following are the sample code.

 
 public static void RegisterBundles(BundleCollection bundles) 
        { 
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include( 
                        "~/Scripts/jquery-{version}.js")); 
 
            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include( 
                        "~/Scripts/jquery-ui-{version}.js")); 
 
            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( 
                        "~/Scripts/jquery.unobtrusive*", 
                        "~/Scripts/jquery.validate*", 
                        “~/Scripts/my_custom_js.js”)); 
 
            // Use the development version of Modernizr to develop with and learn from. Then, when you're 
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need. 
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( 
                        "~/Scripts/modernizr-*")); 
 
            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); 
 
            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include( 
                        "~/Content/themes/base/jquery.ui.core.css", 
                        "~/Content/themes/base/jquery.ui.resizable.css", 
                        "~/Content/themes/base/jquery.ui.selectable.css", 
                        "~/Content/themes/base/jquery.ui.accordion.css", 
                        "~/Content/themes/base/jquery.ui.autocomplete.css", 
                        "~/Content/themes/base/jquery.ui.button.css", 
                        "~/Content/themes/base/jquery.ui.dialog.css", 
                        "~/Content/themes/base/jquery.ui.slider.css", 
                        "~/Content/themes/base/jquery.ui.tabs.css", 
                        "~/Content/themes/base/jquery.ui.datepicker.css", 
                        "~/Content/themes/base/jquery.ui.progressbar.css", 
                        "~/Content/themes/base/jquery.ui.theme.css", 
   "~/Content/themes/base/custome_theme.css")); 
        } 
    } 

ASP.NET MVC 4 Bundling & Minification CDN & Debug Mode Support:

It is very hard to debug the minified js & css file, when debugging your ASP.NET MVC 4 Web Application you would rather the CSS and JavaScript not be bundled and minified for testing. If you want to debug your js file you can use following code, which allow us to enable or disable the features that whether we want to minify our js & css file. This feature is very useful for developers. You can also use CDN by using given code below.

 
BundleTable.EnableOptimizations = true;  
bundles.UseCdn = true; 
   bundles.Add(new ScriptBundle("~/bundles/jquery", 
   @"//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.js" 
   ).Include( 
    "~/Scripts/jquery-{version}.js"));

As CDN is used in release version, to test application we need to set Debug="False” in our web.config check that in local machine.

Summary: The bundling and minification in ASP.NET MVC 4 will help increase the performance of your ASP.NET MVC 4 Websites by reducing the number and size of HTTP Requests to the server. This article shows how can we add our custom css & js file for bundling and minification in ASP.NET MVC 4. If you are looking for Bundling and minification of CSS and JavaScript in ASP.NET MVC 6, click here to read the article.

Keen to hear from you...!

If you have any questions related to what's mentioned in the article or need help with any issue, ask it, I would love to here from you. Please MakeUseOf Contact and i will be more than happy to help.

About the author

Anil Sharma is Chief Editor of dotnet-stuff.com. He's a software professional and loves to work with Microsoft .Net. He's usually writes articles about .Net related technologies and here to shares his experiences, personal notes, Tutorials, Examples, Problems & Solutions, Code Snippets, Reference Manual and Resources with C#, Asp.Net, Linq , Ajax, MVC, Entity Framework, WCF, SQL Server, jQuery, Visual Studio and much more...!!!

blog comments powered by Disqus