{version} wildcard in MVC4 Bundle
Solution 1
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.
Solution 2
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
Related videos on Youtube
Ricardo Polo Jaramillo
Updated on May 06, 2020Comments
-
Ricardo Polo Jaramillo about 4 years
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")); }
-
pavsaund about 11 yearsHow would this work with multiple versions of ex: jquery present in the folder?
-
Hao Kung about 11 yearsIt'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.
-
pavsaund about 11 yearsright, makes sense. If you need specific versions, then make specific bundles, otherwise be version-agnostic. Thx @hao kung
-
Michael Nelson over 10 yearsNote 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."
-
Hao Kung over 10 yearsYes it only works for the last part of the path, basically the filename.
-
gerleim about 10 yearsSeems like a strange implementation to me. - Hidden, used as a magic string. (It's in internal PatternHelper.cs)
-
Zapnologica over 9 yearsvote up for not working in a directory. Any solutions to get it working in a directory?
-
Triynko over 8 yearsAs long as you make sure the old versions are actually removed.
-
Jon Koeter over 7 yearsThis 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 over 7 yearsEdit: nevermind, they clearly explain it here! asp.net/mvc/overview/performance/bundling-and-minification Thank you!
-
Marc Roussel over 6 yearsWhen using {version} I receive Jquery is undefined and when I replace with the actual version, in my case 3.1.1 it works fine.
-
AlvinfromDiaspar over 5 yearsWhat 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.
-
Zar Shardan almost 5 years{version} also handles .min and non-min versions properly.
-
Suncat2000 about 4 years@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?