DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Advanced Options of ASP.NET Bundling and Minification

Advanced Options of ASP.NET Bundling and Minification

Imran Baloch user avatar by
Imran Baloch
·
Oct. 07, 12 · Interview
Like (0)
Save
Tweet
Share
6.39K Views

Join the DZone community and get the full member experience.

Join For Free
Server.MapPath is one of the most-used methods in ASP.NET. Lot of libraries/assemblies has used this method. The problem with using this method inside a library is that it will tie the users of this library with a ASP.NET intrinsic Server object that 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 to a web page, for example, *.intellisense.js and *-vsdoc.js script files which are used by Visual Studio for intellisense. There are some other script 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,

public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
{
	if (ignoreList != null)
	{
		ignoreList.Ignore("*.intellisense.js");
		ignoreList.Ignore("*-vsdoc.js");
		ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
		ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
		ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
		return;
	}
	else
	{
		throw new ArgumentNullException("ignoreList");
	}
}

You can easily override this by using the 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,

public static void AddDefaultFileOrderings(IList<bundlefilesetordering> list)
{
	if (list != null)
	{
		BundleFileSetOrdering bundleFileSetOrdering = new BundleFileSetOrdering("css");
		bundleFileSetOrdering.Files.Add("reset.css");
		bundleFileSetOrdering.Files.Add("normalize.css");
		list.Add(bundleFileSetOrdering);
		BundleFileSetOrdering bundleFileSetOrdering1 = new BundleFileSetOrdering("jquery");
		bundleFileSetOrdering1.Files.Add("jquery.js");
		bundleFileSetOrdering1.Files.Add("jquery-min.js");
		bundleFileSetOrdering1.Files.Add("jquery-*");
		bundleFileSetOrdering1.Files.Add("jquery-ui*");
		bundleFileSetOrdering1.Files.Add("jquery.ui*");
		bundleFileSetOrdering1.Files.Add("jquery.unobtrusive*");
		bundleFileSetOrdering1.Files.Add("jquery.validate*");
		list.Add(bundleFileSetOrdering1);
		BundleFileSetOrdering bundleFileSetOrdering2 = new BundleFileSetOrdering("modernizr");
		bundleFileSetOrdering2.Files.Add("modernizr-*");
		list.Add(bundleFileSetOrdering2);
		BundleFileSetOrdering bundleFileSetOrdering3 = new BundleFileSetOrdering("dojo");
		bundleFileSetOrdering3.Files.Add("dojo.*");
		list.Add(bundleFileSetOrdering3);
		BundleFileSetOrdering bundleFileSetOrdering4 = new BundleFileSetOrdering("moo");
		bundleFileSetOrdering4.Files.Add("mootools-core*");
		bundleFileSetOrdering4.Files.Add("mootools-*");
		list.Add(bundleFileSetOrdering4);
		BundleFileSetOrdering bundleFileSetOrdering5 = new BundleFileSetOrdering("prototype");
		bundleFileSetOrdering5.Files.Add("prototype.js");
		bundleFileSetOrdering5.Files.Add("prototype-*");
		bundleFileSetOrdering5.Files.Add("scriptaculous-*");
		list.Add(bundleFileSetOrdering5);
		BundleFileSetOrdering bundleFileSetOrdering6 = new BundleFileSetOrdering("ext");
		bundleFileSetOrdering6.Files.Add("ext.js");
		bundleFileSetOrdering6.Files.Add("ext-*");
		list.Add(bundleFileSetOrdering6);
		return;
	}
	else
	{
		throw new ArgumentNullException("list");
	}
}

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 what this property contains by default, 

public static void AddDefaultFileExtensionReplacements(FileExtensionReplacementList list)
{
	if (list != null)
	{
		list.Add("min", OptimizationMode.WhenEnabled);
		list.Add("debug", OptimizationMode.WhenDisabled);
		return;
	}
	else
	{
		throw new ArgumentNullException("list");
	}
}

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.

 

ASP.NET

Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Explaining: MVP vs. PoC vs. Prototype
  • Internal Components of Apache ZooKeeper and Their Importance
  • DevOps Roadmap for 2022
  • How to Use MQTT in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: