Saturday, March 31, 2012

Configuring Bundles in MVC 4

We write a lot of JavaScript.

Thus the bundling, compression, and minification of JavaScript is important to the speed and performance of modern websites. This is why I love and have been a big advocate of tools like Combres, and also why I was so excited to hear that such features were (finally) coming built in to ASP.NET MVC 4.

Introducing MvcBundleConfig

MvcBundleConfig is a very simple minimalist project I wrote to add configuration and debugging features to MVC 4's bundling framework, and achieves all 6 six of the goals listed below. It requires only MVC4 to run, and you need only add one config file to your project, one line of code to your application start, and you are off and running.

NuGet Package:

Source on GitHub:

Before we get to the demonstration at the bottom, let's review the needs and wants of a good minification framework.

What I NEED in a minification tool:

  1. Compress resources into single files.
    • Multiple request take time and resources, neither of which are things that any of us have to spare. By compressing resources into single requests and can limit the overhead and load time on both our clients and our servers.
  2. Minify JavaScript and CSS content.
    • Minification removes as many unnecessary white spaces and characters as possible from your resource files, reducing file size by up to 80% on average. When then compounded with gzip, we can reduce the file size another 50%. This means that our web applications can be brought down to clients 90% faster.
  3. Make use of both client and server side caching.
    • Making 90% less requests is nice, and making those requests 90% smaller is even better, but only ever having to request or serve them once is the key to true performance. Unfortunately client and server caching can be a bit complex due to quirks of different technologies and browsers. A good minification framework should abstract these concerns away from us.
  4. Ability to turn off during debugging.
    • As fantastic as everything that we have listed about is for a production website, it is a nightmare for a development website. Debugging JavaScript is no less complicated or time consuming than debugging C#, and we need to be able to use a debuggers and other client side tools that are inhibited by minification. A good minification framework must expose a debugging mode that skips compression pipeline.

What I WANT in a minification tool:

  1. Simple and dynamic configuration.
    • I hate hardcoded configuration. It bloats my code, and it requires bin drops to deploy. Meanwhile I really like the ability to add simple configuration files to my site as often as I can. Config files are explicit, abstract, and can be updated at any time. Win.
  2. Take a few dependencies as possible.
    • I mentioned above that I like Combres and it has a reasonably sized code base, unfortunately the fact that it's NuGet package pulls down half a dozen additional dependencies makes it feel quite heavy. The fewer dependencies a framework takes the better.

MvcBundleConfig Examples


<?xml version="1.0"?>
<bundleConfig ignoreIfDebug="true" ignoreIfLocal="true">
    <add bundlePath="~/css/shared">
        <add directoryPath="~/content/" searchPattern="*.css" />
    <add bundlePath="~/js/shared" minify="false">
        <add filePath="~/scripts/jscript1.js" />
        <add filePath="~/scripts/jscript2.js" />


protected void Application_Start()
    // Only code required for MvcBundleConfig wire up


<!DOCTYPE html>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width" />
        @* Call bundle helpers from MvcBundleConfig *@

In the Browser

NuGet Package:

Source on GitHub:

kick it on



  1. This is really cool indeed. I thought about the same after intitial investigatation of MVC4 B&M feature.

    What I don't like if .config file. This is so old-school :)..

  2. Alex,

    If you don't want to use the config files I have added some simple collections that you can easily instantiate to wire up the configuration programmatically.

    Config files may be old school, but they are very effective.


  3. Hi Tom. You mention GZip in point 2, but I'm not sure how that would work in this solution. Would you mind expanding on that?

  4. Qube,

    Modern browsers support resources that has been compressed. Just like zipping files to save space on disk, your server can gzip resources to make the smaller (and thus faster) when answering requests.

    The MVC bundling will take care of minification, and IIS can take care of the zipping:


  5. This is good stuff! Thanks for your work on this. You should publish a Nuget package for it.


    Nathan Bridgewater

  6. A Nuget package would be a really good idea for this...

  7. Question - do you have a way to force inclusion order? I have a file with a dependency on another file in a "library" directory. The dependent file is being loaded first, but it needs to load after. ex)

    {add bundlePath="~/js/shared" minify="true"}
    {add directoryPath="~/Scripts/lib" searchPattern="*.js"}{/add}
    {add filePath="~/scripts/jquery.somethingdependent.js"}{/add}

    Or do I just have to split up my bundles and explicitly declare them in the correct order? Thanks.

  8. This comment has been removed by the author.

  9. Tom, great work! A couple of questions if I may ask:
    1) How do you enforce the bundling order?
    2) where in your code would you add so that the bundling url is prefixed with a CDN url as in --> bundleConfig ignoreIfDebug="true" ignoreIfLocal="true" cdnUrl="htt://"

    Any insight is appreciated.

  10. NuGet Package:

    I do not believe that you can specify the inclusion order of a directory, but of course you can control the order by including the files.

    I am not sure that MVC4 has CDN support, I'll have to look that up.

  11. Tom, sorry, let me clarify:

    my single file `jquery.somethingdependent.js` requires that jQuery be present. jQuery is one of the files in '~/Scripts/lib'. According to @ScottGu they should load alphabetically, then get automatically rearranged so that libraries like jQuery are loaded first (

    However, in my case, the dependent file is being included first, then the 'lib' folder. I was actually asking two things - does your extension alter this behavior (unintentionally?), and/or how could we add some xml attribute to the config file to enforce loading order? Perhaps in `CreateBundleConfigs` or `AddBundleConfiguration`?

    1. Well, a little more digging shows that the "culprit" was `AddBundleConfiguration` in '\MvcBundleConfig\Extensions\BundleCollectionExtensions.cs'. It loaded each file from the bundle, *then* looped the directories to add them. This caused the dependent file to appear first in the bundle. I switched them, then recompiled your extension, and it fixed my problem.

      My question then becomes - what should the expected behavior be? Is your extension interfering with the default bundle inclusion logic mentioned by @ScottGu? Are you then supposed to reimplement the autosorting magic from the regular bundling?

      Regardless, your extension is awesome. I could probably avoid my issue by renaming my files with explicit alphabetical ordering ("js1-myfile.js, js2-anotherfile.js..."), and putting them in "presorted" folders. If that's the case, I'd suggest mentioning it in your documentation.

    2. Thanks for peeking into the code and coming up with a solution. As far as expected behavior, I'll probably just start with your use case.

      You can expect an update sometime next week. :)

      Thanks Jeremy!

  12. Hi, this is great work. Did you add in CDN Support ?

  13. I like it, it's cool. Really helped to speed me up. Is there an xsd file I should be including somewhere though? The warnings get annoying.

  14. Dewald,
    I have added CDN support in v2.1.0. :)

    I have not taken the time to create a XSD, put please feel free to make a pull request against the GitHub project.

    1. Can you please show how the bundle.config should look like with CDN support added?

    2. This comment has been removed by the author.

    3. The code makes it look like you add a "cdnPath" attribute to the "add bundlePath" node, but that doesn't appear to be working

  15. This comment has been removed by the author.

  16. This comment has been removed by the author.

  17. With cdnPath we have to list each file individually. I went back from this solution to the solution specified here


  18. Hi Tom,

    Can i manage which version to be use. I mean there are more than one JS file with version on it. But i want to choose one of them with out modify.

    Sorry for my bad english.


  19. Tom,

    Getting an error trying to turn off minification.


    Server Error in '/' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 59: @Html.JsBundle("~/js/types")
    Line 60: @Html.JsBundle("~/js/libraries")
    Line 61: @Html.JsBundle("~/js/shared") <--------- Offending line

    If I remove the minify it works correctly, if I set minify to "true" it also works correctly. Could you take a look?


  20. And it stripped my config. Here it is again (minus the aligators)

    add bundlePath="~/js/shared" minify="false"
    add directoryPath="~/Scripts/Shared" searchPattern="*.js"

    1. Should be fixed in v2.2.0.


Real Time Web Analytics