Extending System.Web.Optimization

The ASP.NET framework is packed with loads of great features and with each release it seems to get better and better. One of my favourite features is the bundling and minification classes that optimize JavaScript and CSS files in a website to reduce file size and improve page performance.

ASP.NET logo

By bundling and minifying your scripts and styles, you can significantly improve your page load times. While the built in classes in the System.Web.Optimization namespace are easy to use, I find that they aren't very flexible. HTML5 has a few additional attributes that allow you to do many more things with your page functionality, but currently you can't use these attributes with the built in System.Web.Optimization classes. I wanted to be able to add extra HTML attributes to the tags while still using the built in functionality that the framework provides. I created the class below that extends the current ASP.NET optimization functionality, yet still allows you some flexibility.

Simply drop the above code into your project and it's ready to go. Say for example you wanted to include the async attribute on your JavaScript tag, using the code above, you simply call it like so:

@Bundler.RenderScripts("~/Js/Spaces", new { async = "true" })

Which will produce the following script tag: