ASP.NET MVC - Bundle Config order
To reduce the codes during creating bundles, I suggest you create an extension method.
Require infrastructure classes:
class NonOrderingBundleOrderer : IBundleOrderer
{
public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
{
return files;
}
}
static class BundleExtentions
{
public static Bundle NonOrdering(this Bundle bundle)
{
bundle.Orderer=new NonOrderingBundleOrderer();
return bundle;
}
}
Now simply use it like this:
All in one command ð
bundles.Add(new ScriptBundle("~/bundles/jqueryval")
.NonOrdering()
.Include(
"~/Scripts/globalize/globalize.js",
"~/Scripts/globalize/cultures/globalize.culture.es-CL.js",
//...
);
By default, bundling order is alphabetical for names with wildcards (as pointed out in the comments). However, it also orders based on what it thinks your dependency tree is, and jQuery
scripts seem to get slotted to the top. You need to create an object that implement IBundleOrder
:
class NonOrderingBundleOrderer : IBundleOrderer
{
public IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
{
return files;
}
}
This prevents the default ordering. Now to use it:
var bundle = new ScriptBundle("~/bundles/globalization")
.Include("~/Scripts/globalize/globalize.js")
.Include("~/Scripts/globalize/cultures/globalize.culture.es-CL.js")
.Include("~/Scripts/jquery.validate.globalize.js");
bundle.Orderer = new NonOrderingBundleOrderer();
bundles.Add(bundle);
ref: http://stevescodingblog.co.uk/changing-the-ordering-for-single-bundles-in-asp-net-4/
For further reading, an answer to MikeSmithDev's question provides further insight into the default ordering for popular script libraries:
Ordering of Files within a bundle - What are the known libraries?
In the last version of MVC 5 (at october 27 of 2014), yo should use this class instead:
class AsIsBundleOrderer : IBundleOrderer
{
public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
{
return files;
}
}
And create the bundle like the other response:
var bundle = new ScriptBundle("~/bundles/globalization")
.Include("~/Scripts/globalize/globalize.js")
.Include("~/Scripts/globalize/cultures/globalize.culture.es-CL.js")
.Include("~/Scripts/jquery.validate.globalize.js");
bundle.Orderer = new AsIsBundleOrderer();
bundles.Add(bundle);