Recently we upgraded an MVC 3 application to MVC4 and wanted to take advantage of the new bundling and minification functionality for our css and js files. There are some great articles on the asp.net site about how to use all of these new features:
Initially I tested it out on a brand new MVC 4 project I created in VS2012. It worked exactly as advertised, so I was anxious to get it working in one of our actual production web applications. However, my first attempts at adding bundling to our application left me a little frustrated.
When I tried to add the example code for using BundleConfig and BundleTable to my Global.asax, these classes could not be found. That seemed a little strange since this was a working MVC4 application. Why were the references available in the brand new application I had created (based on the MVC4 application template) and not in my upgraded MVC4 application?
After doing a little research I realized that the MVC4 template in VS had references to Nuget packages which included this functionality. So the new optimization framework was not part of the MVC bits, but rather an add-on package. This actually made a lot of sense because it allows it to easily be shared with other types of ASP.NET projects and to be deployed in isolation. I just wasn’t expecting this initially, so it took me a little time to figure out where to get the bits I needed to add this functionality to our application.
To add bundling and minification to an existing MVC application you need to add the following Nuget packages:
I would recommend grabbing the latest versions of these packages. Once you’ve done that then the rest should work as advertised. Enjoy!