Home > MVC > Bundling and minification in MVC3 and Asp.Net 4.0

Bundling and minification in MVC3 and Asp.Net 4.0


Adding references:

First of all add the references of System.Web.Optimization.dll and WebGrease.dll to your MVC3 and Asp.Net 4.0 projects as shown below. You can download the dll by using download link.


Creating Bundle:

Now create the bundle for your css and js files with in the Global.asax file as shown below.

Here, I have created the bundle of all required css and js files. You can also add your own css and js files with complete path using Include method.

Registering Bundle:

Adding Bundles to Layout Page in MVC3:

Now you can add the above created style and script bundles to the Layout page or where you want to use as shown below:

Adding Bundles to Master Page in Asp.Net 4.0:

Now you can add the above created style and script bundles to the Master page or where you want to use as shown below:

In Asp.Net 4.0 you also required to add System.Web.Optimization namespace and assembly Microsoft.AspNet.Web.Optimization.WebForms reference to the web.config file of your Asp.Net 4.0 project as shown below:

You need not to do any changes in web.config file of your MVC3 project.

Enabling Bundling and Minification in debug mode

Bundling and minification doesn’t work in debug mode. So to enable this features you need to add below line of code with in Application_Start event of Global.asax.

How it works..

Now run your application and you will see that all the css and js files are converted to single css and js file as shown below:

Minification

Minification is technique for removing unnecessary characters (like white space, newline, tab) and comments from the JavaScript and CSS files to reduce the size which cause improved load times of a webpage. There are so many tools for minifying the js and css files. JSMin and YUI Compressor are two most popular tools for minifying the js and css files. Use these tools for minifiying your css and js files and use in your application with “.min” suffix. So that you can easily identified that this is a minimize version of your css or js file.

Hope it helps!

Advertisements
  1. Hugh Jackman
    May 13, 2013 at 9:44 am

    Hi!

    Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the
    number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)
    So thanks for sharing this important information through this article. There are few other links that have described for manage “Bundling and minification”.

    http://www.mindstick.com/Articles/2d3da74a-94da-45fe-b361-97263cd725f2/?Bundling%20and%20Minification%20in%20ASP%20NET%20MVC%204

    http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: