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"));
}
Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83

3 Answers3

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.
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
Hao Kung
  • 28,040
  • 6
  • 84
  • 93
  • 2
    How would this work with multiple versions of ex: jquery present in the folder? – pavsaund Mar 08 '13 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 08 '13 at 17:45
  • right, makes sense. If you need specific versions, then make specific bundles, otherwise be version-agnostic. Thx @hao kung – pavsaund Mar 11 '13 at 07:41
  • 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." – Michael Nelson Oct 08 '13 at 17:14
  • Yes it only works for the last part of the path, basically the filename. – Hao Kung Oct 08 '13 at 19:06
  • 1
    Seems like a strange implementation to me. - Hidden, used as a magic string. (It's in internal PatternHelper.cs) – gerleim Apr 25 '14 at 11:14
  • 4
    vote up for not working in a directory. Any solutions to get it working in a directory? – Zapnologica Nov 27 '14 at 07:37
  • This might be a silly question, but I always used the the * to join the js in debug and the min.js in production. Using {version}* doesn't work (giving me the error "Wildcards are only allowed in the last path segment, can contain only one leading or trailing wildcard, and cannot be used with {version}."). Does that mean I can no longer benefit from the flexible include when I use {version} or is there a better way? – Jon Koeter Sep 12 '16 at 07:11
  • 3
    Edit: nevermind, they clearly explain it here! http://www.asp.net/mvc/overview/performance/bundling-and-minification Thank you! – Jon Koeter Sep 12 '16 at 07:23
  • 1
    What invokes this regex to get the proper {version} number? And where/what is the source of this {version} come from? I agree with @gerleim, seems like too much "magic" behind the scenes. – AlvinfromDiaspar Oct 02 '18 at 17:20
  • {version} also handles .min and non-min versions properly. – Zar Shardan Jun 26 '19 at 17:42
  • @JonKoeter Um, _not_ clearly explained. he only thing I got out of that was: "blah, blah, blah, _appropriate version of jQuery in your Scripts folder_. How is "appropriate" determined? – Suncat2000 Feb 19 '20 at 20:12
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

MUG4N
  • 19,377
  • 11
  • 56
  • 83
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.

VahidNaderi
  • 2,448
  • 1
  • 23
  • 35
leoli
  • 1