Take the 2-minute tour ×

Is there a way to bundle a single file using the new bundling features in MVC4? I know bundling a single file doesn't make much sense but I'd like to use server-side minification and let MVC append a hash on to the end of the URL for caching purposes.

I've tried the @Scripts.Url("~/Scripts/myscript.js") but it doesn't appear to work.

share|improve this question
add comment

3 Answers

Well, define a bundle containing only this file in your bundle config:

    new ScriptBundle("~/bundles/myscript").Include("~/Scripts/myscript.js")

and then use it in your view by referencing it by name:

share|improve this answer
Problem is our site is quite dynamic as we have many-many white labels so adding to the BundleTable in Global.asax file is not an option - is there a way to do it on the fly? –  eth0 Oct 9 '12 at 16:25
What do you mean by on the fly? Please provide more details. If you have many scripts you should consider grouping them in folders and bundling them per functionality. –  Darin Dimitrov Oct 9 '12 at 16:25
It seems I can only register a bundle on application start-up. If I try the above code outside of my Global.axax file it doesn't work. I need to be able to "register" a bundle at any time during the application, does that make more sense? –  eth0 Oct 9 '12 at 16:30
When/why do you need to register bundles outside from your Application_Start? –  Darin Dimitrov Oct 9 '12 at 16:33
Because our site is quite dynamic and we have hundreds of different white labels/skins and each "skin" has a bunch of Javascript files which change every day. It would be a nightmare to manage to put every single CSS/JS file in AppStart –  eth0 Oct 10 '12 at 7:43
add comment

I dont know, if I get you right, but you can add bundles any time you want to, just add it to the bundlecollection:

BundleTable.Bundles.Add(new ScriptBundle("~/Scripts/myBundleName").Include(

I am not sure how you planing to include the bundle on client side, but after a bundle is registered you can get the url of the minified script file with this code:

string url = BundleTable.Bundles.ResolveBundleUrl("~/Scripts/myBundleName");

In my case I just get this url through an ajax request and create a script tag on client side. This way the script file will be minified and the url comes with an hash code, so the browser should cache it and when the bundle is changed should load the newer version.

I hope this is, what you intended to do.

share|improve this answer
add comment

I needed something like this, and here's what I came up with

On a view I can add a script or style with an HTML Helper. These will each accept n number of parameters.


So I created an extention to handle this for me. Here's what the class looks like.

using System.Linq;
using System.Web.Optimization;

namespace System.Web.Mvc
    public static class HtmlHelperExtensions

        public static IHtmlString Script(this HtmlHelper helper, params string[] urls)
        var bundleDirectory = "~/Scripts/bundles/" + MakeBundleName(".js", urls);
        var thisBundle = new ScriptBundle(bundleDirectory).Include(urls);

        return Scripts.Render(bundleDirectory);

    public static IHtmlString Style(this HtmlHelper helper, params string[] urls)
        var bundleDirectory = "~/Styles/bundles/" + MakeBundleName(".css", urls);
        var thisBundle = new StyleBundle(bundleDirectory).Include(urls);

        return Styles.Render(bundleDirectory);

    private static string MakeBundleName(string type, params string[] urls)
        var bundleSections = new List<string>();

        foreach (var item in urls)
            bundleSections.Add(item.Replace("~/", "").Replace("/", ".").Replace(type, ""));

        return string.Join("+", bundleSections.ToArray());


This does hard code where the bundle "virtual directory" exists. So if you have any relative paths to images or anything else in your files, they will break. For our use case, this works fine for us for now.

The bundle names are auto-generated. Again, this works for us, but it could be better. I could see a simple improvement to this where you must provide a bundle path & name that would have a syntax like this: @Html.Script("~/bundles/myBundleName","~/Scripts/foo.js", "~/Scripts/bar.js")

I'd be happy to hear about any improvements anyone might have for this.

share|improve this answer
thanks I was just going down the same path and glad someone else went this route –  B Z Jun 5 '13 at 12:51
you should also add using System.Collections.Generic; –  xoemab Jun 26 '13 at 18:59
add comment

Your Answer


