ASP.NET 4.5 Bundling and Minification Update

Share this article

Recently I wrote an article on the new bundling and minification features in ASP.NET 4.5. Out of the box, you get bundling and minifincation without really needing to do anything. If you missed it you can find it here. Since that article, there have been some changes to ASP.NET 4.5. Previously you needed to manually add the following line of code in the Global.asax file:

protected void Application_Start() 
{
    BundleTable.Bundles.EnableDefaultBundles();
}
Since the update to ASP.NET 4.5, you no longer need to manually add that line of code. It’s automatically there when you begin a new project.
protected void Application_Start()
{
    BundleTable.Bundles.RegisterTemplateBundles();
}
The code for referencing the bundled and minified CSS and JavaScript files has been updated too. Previously to reference a bundle, you’d add the following code:

<script src="scripts/js"></script>
<link href="content/css" rel="stylesheet" />

This is assuming you have your JavaScript files in a folder called scripts, and your style sheets are in a folder called content
. This still works with this new release. The problem with referencing the files this way is when you update the files, the old versions will still be cached either by your browser, or by a web proxy. By default the files are cached for 1 year, this is great if you never update them! Microsoft has addressed this issue by allowing you to reference the bundled and minified files via ResolveBundleUrl.

<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>
<link href="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Content/css")" rel="stylesheet" type="text/css" />
The benefit of using ResolveBundleUrl is that it appends a magic string at the end of the file name. This magic string only changes when the file changes, meaning the files will be cached for 1 year or until they change. If you inspect the page through the developer tools in Google Chrome, you’ll see the magic string. You can include a @using
keyword to remove the need to fully qualify the System.Web.Optimization assembly.

@using System.Web.Optimization

Be certain to remember the use of this feature when developing with ASP.NET 4.5.

Frequently Asked Questions (FAQs) about ASP.NET 4.5 Bundling and Minification Update

What is the purpose of bundling and minification in ASP.NET 4.5?

Bundling and minification are two techniques used in ASP.NET 4.5 to improve request load time and performance. Bundling helps in reducing the number of HTTP requests made by a webpage by combining multiple files into a single bundle. Minification, on the other hand, reduces the size of the requested resources by removing unnecessary characters like white spaces, comments, and line breaks from the code. These techniques help in improving the speed and efficiency of ASP.NET applications.

How can I enable bundling and minification in my ASP.NET application?

To enable bundling and minification in your ASP.NET application, you need to create a new Bundle object for each bundle you want to create in the BundleConfig.cs file. Then, use the Include method to add scripts or stylesheets to the bundle. Finally, call the RegisterBundles method of the BundleTable class in the Application_Start method in the Global.asax.cs file.

What is the difference between the ‘Include’ and ‘IncludeDirectory’ methods in bundling?

The ‘Include’ method is used to add individual files to a bundle. You need to specify the exact path of each file you want to add. On the other hand, the ‘IncludeDirectory’ method allows you to add all the files in a directory to a bundle. You just need to specify the path of the directory, and all the files in that directory will be added to the bundle.

How can I use the ‘System.Web.Optimization’ namespace in my application?

To use the ‘System.Web.Optimization’ namespace in your application, you need to add a reference to it in your project. You can do this by right-clicking on the ‘References’ folder in the Solution Explorer, selecting ‘Add Reference’, and then selecting ‘System.Web.Optimization’ from the list of available references.

What is the role of the ‘BundleTable’ class in ASP.NET bundling and minification?

The ‘BundleTable’ class is a static class that provides access to the collection of registered bundles in an ASP.NET application. It has a ‘Bundles’ property that returns a ‘BundleCollection’ object, which represents all the bundles registered in the application. You can use this class to register, retrieve, or remove bundles.

Can I use bundling and minification with MVC applications?

Yes, you can use bundling and minification with MVC applications. In fact, these techniques are especially useful in MVC applications, as they often have many scripts and stylesheets. By bundling and minifying these resources, you can significantly improve the performance of your MVC application.

How can I disable bundling and minification for debugging purposes?

You can disable bundling and minification by setting the ‘EnableOptimizations’ property of the ‘BundleTable’ class to ‘false’. This will serve the individual, unminified files, which can be useful for debugging purposes.

What is the ‘Microsoft.AspNet.Web.Optimization’ package?

The ‘Microsoft.AspNet.Web.Optimization’ package is a NuGet package that provides support for bundling and minification in ASP.NET applications. It includes the ‘System.Web.Optimization’ namespace, which contains all the classes and methods needed for bundling and minification.

Can I use bundling and minification with Web Forms applications?

Yes, you can use bundling and minification with Web Forms applications. Just like with MVC applications, these techniques can help improve the performance of your Web Forms application by reducing the number of HTTP requests and the size of the requested resources.

How does bundling and minification affect the load time of a webpage?

Bundling and minification can significantly reduce the load time of a webpage. By combining multiple files into a single bundle, bundling reduces the number of HTTP requests made by the webpage. Minification reduces the size of the requested resources by removing unnecessary characters from the code. Both of these techniques result in faster page load times and a better user experience.

Malcolm SheridanMalcolm Sheridan
View Author

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, ASPInsider, Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Follow him on twitter @malcolmsheridan.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week