{version} wildcard in MVC4 Bundle
Asked Answered
B

3

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"));
}
Babette answered 19/8, 2012 at 19:4 Comment(0)
S
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.
Sika answered 20/8, 2012 at 17:4 Comment(12)
How would this work with multiple versions of ex: jquery present in the folder?Kenrick
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.Sika
right, makes sense. If you need specific versions, then make specific bundles, otherwise be version-agnostic. Thx @hao kungKenrick
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."Devoirs
Yes it only works for the last part of the path, basically the filename.Sika
Seems like a strange implementation to me. - Hidden, used as a magic string. (It's in internal PatternHelper.cs)Keare
vote up for not working in a directory. Any solutions to get it working in a directory?Hairraising
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?Heartbreaking
Edit: nevermind, they clearly explain it here! asp.net/mvc/overview/performance/bundling-and-minification Thank you!Heartbreaking
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.Nonalignment
{version} also handles .min and non-min versions properly.Himself
@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?Envoi
S
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

Spectre answered 19/8, 2012 at 20:39 Comment(2)
As long as you make sure the old versions are actually removed.Isley
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.Canada
B
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.

Benbena answered 5/5, 2019 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.