158

In MVC 4 we have bundles. While defining the bundles we can use wildcards like * for all files in a folder.

In the example below what does -{version} mean?

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-{version}.js"));
}
0

3 Answers 3

183

The -{version} basically maps to a version regex, or to be precise: (\d+(?:\.\d+){1,3}).
Using * tends to grab too much, for example if you bundle jquery*, that will include jquery-ui as well which might mess up the ordering. But using jquery-{version}.js would let you avoid having to update your bundle definition every time you upgrade jquery.

Additional things to note:

  • {version} only works for the last part of the path--basically the file name--not a directory.
  • multiple version of jquery in the same folder will all get caught up.
12
  • 2
    How would this work with multiple versions of ex: jquery present in the folder?
    – pavsaund
    Mar 8, 2013 at 12:22
  • 21
    It's a regex so it would include all matches, so if you had multiple versions in the same directory you probably would not want to use this.
    – Hao Kung
    Mar 8, 2013 at 17:45
  • 3
    Note that {version} does not seem to work within a path. At work, we have the bootstrap version in the path (not in the file name), so I am trying to do this: "~/Content/Libraries/bootstrap/{version}/css/bootstrap.css" But when I run RegisterBundles, I get an ArgumentException that says "Directory does not exist." Oct 8, 2013 at 17:14
  • 4
    vote up for not working in a directory. Any solutions to get it working in a directory? Nov 27, 2014 at 7:37
  • 3
    Edit: nevermind, they clearly explain it here! asp.net/mvc/overview/performance/bundling-and-minification Thank you!
    – Jon Koeter
    Sep 12, 2016 at 7:23
12

This bundle is able to accomodate version numbers in script names. So updating jQuery to a new version in your application (via NuGet or manually) doesn't require any code / markup changes.

See the following link for more information on bundling: http://weblogs.asp.net/jgalloway/archive/2012/08/16/asp-net-4-5-asp-net-mvc-4-asp-net-web-pages-2-and-visual-studio-2012-web-developer-features.aspx

2
  • 3
    As long as you make sure the old versions are actually removed.
    – Triynko
    Dec 2, 2015 at 20:02
  • When using {version} I receive Jquery is undefined and when I replace with the actual version, in my case 3.1.1 it works fine. Aug 12, 2017 at 2:49
0

~/Scripts/jquery-{version}.js is included in it. Here bundling system is smart enough to reference the highest version of jquery file when we specified {version} selector in the path. Also, this bundling system is smart enough to pick the minified version of the file, if available at the defined path.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.