Advertisements
Home > MVC > Advanced Options of ASP.NET Bundling and Minification

Advanced Options of ASP.NET Bundling and Minification


Rick Anderson has written a great introduction to ASP.NET Bundling and Minification at here. In addition to the options presented by Rick, there are some other features in System.Web.Optimization framework which is also important for developers to understand in order to make their application performance and architecture good. In this article, I will show you some of the advance stuff of ASP.NET Bundling and Minification.

Description:

Server.MapPath is one of the most used method in ASP.NET. Lot of libraries/assemblies has used this method. The problem of using this method inside a library  is that it will tie the users of this library with ASP.NET intrinsic Server object which makes unit testing a little difficult. Fortunately, System.Web.Optimization have a special property called BundleTable.MapPathMethod which allows users to provide a fake method for unit testing.

There are some script files that you will never expect to add on a web page, for example, *.intellisense.js and *-vsdoc.js script files which are used by Visual Studio for intellisense. There are some other scripts files that you need to ignore during production, like, *.debug.js file. There are some other script files that you will not add during debugging/testing, for e.g, *.min.js and *.min.css files. Luckily, our optimization library will automatically takes care of these situation through BundleCollection.IgnoreList property. Here is the what this property contains by default,

You can easily override this by using BundleCollection.IgnoreList property in BundleConfig.cs file. Note: it is also important that you should be aware about a bug in the implementation of this.

The ordering of scripts and css files is also very important. For example, if you have jquery.js and jquery.ui.js then jquery.js file should be appear before jquery.ui.js. Similarly for css, reset.css should appear first(if exist) and then normalize.css(if exist) and then the remaining ones. Fortunately again, our optimization assembly will also handle this scenario by using BundleCollection.FileSetOrderList property. You can also override this inside BundleConfig.cs file. Here is the what this property contains by default,

Now let’s say that you have only registered the jquery-1.6.2.js(not jquery-1.6.2.min.js). The optimization framework will still pick the jquery-1.6.2.min.js instead of jquery-1.6.2.js during production. This is possible via BundleCollection.FileExtensionReplacementList property. Like others, you can also override this inside BundleConfig.cs file. Here is the this property contains by default,

For clearing all these settings, you just need to call BundleCollection.ResetAll method. This method will clear all the above lists and empty the registered bundles.

The System.Web.Optimization framework also allows you to define a custom orderer for a bundle. You just need to implement the IBundleOrderer.OrderFiles method in a new class and set the Bundle.Orderer property. Here is an example.

Advertisements
Categories: MVC
  1. No comments yet.
  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: