{version} wildcard in MVC4 Bundle

54,597

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

Share:
54,597

Related videos on Youtube

Ricardo Polo Jaramillo
Author by

Ricardo Polo Jaramillo

Updated on May 06, 2020

Comments

  • Ricardo Polo Jaramillo
    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
    pavsaund about 11 years
    How would this work with multiple versions of ex: jquery present in the folder?
  • Hao Kung
    Hao Kung about 11 years
    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.
  • pavsaund
    pavsaund about 11 years
    right, makes sense. If you need specific versions, then make specific bundles, otherwise be version-agnostic. Thx @hao kung
  • Michael Nelson
    Michael Nelson over 10 years
    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."
  • Hao Kung
    Hao Kung over 10 years
    Yes it only works for the last part of the path, basically the filename.
  • gerleim
    gerleim about 10 years
    Seems like a strange implementation to me. - Hidden, used as a magic string. (It's in internal PatternHelper.cs)
  • Zapnologica
    Zapnologica over 9 years
    vote up for not working in a directory. Any solutions to get it working in a directory?
  • Triynko
    Triynko over 8 years
    As long as you make sure the old versions are actually removed.
  • Jon Koeter
    Jon Koeter over 7 years
    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
    Jon Koeter over 7 years
    Edit: nevermind, they clearly explain it here! asp.net/mvc/overview/performance/bundling-and-minification Thank you!
  • Marc Roussel
    Marc Roussel over 6 years
    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.
  • AlvinfromDiaspar
    AlvinfromDiaspar over 5 years
    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.
  • Zar Shardan
    Zar Shardan almost 5 years
    {version} also handles .min and non-min versions properly.
  • Suncat2000
    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?