{version} wildcard in MVC4 Bundle

C#asp.netasp.net Mvcasp.net Mvc-4asp.net Optimization

C# Problem Overview


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"));
}

C# Solutions


Solution 1 - C#

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 - C#

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

Solution 3 - C#

~/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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRicardo Polo JaramilloView Question on Stackoverflow
Solution 1 - C#Hao KungView Answer on Stackoverflow
Solution 2 - C#MUG4NView Answer on Stackoverflow
Solution 3 - C#leoliView Answer on Stackoverflow