C# MVC4 Bundle 中的 {version} 通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12029161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:07:53  来源:igfitidea点击:

{version} wildcard in MVC4 Bundle

c#asp.netasp.net-mvcasp.net-mvc-4asp.net-optimization

提问by Ricardo Polo Jaramillo

In MVC 4 we have bundles. While defining the bundles we can use wildcards like * for all files in a folder.

在 MVC 4 中,我们有包。在定义包时,我们可以对文件夹中的所有文件使用诸如 * 之类的通配符。

In the example below what does -{version}mean?

在下面的例子中是什么-{version}意思?

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-{version}.js"));
}

采纳答案by Hao Kung

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-uias well which might mess up the ordering. But using jquery-{version}.jswould let you avoid having to update your bundle definition every time you upgrade jquery.

-{version}基本映射到一个版本的正则表达式,或者更确切地说:(\d+(?:\.\d+){1,3})
使用*往往会占用太多,例如,如果您 bundle jquery*,那也会包括jquery-ui可能会弄乱排序的内容。但是使用jquery-{version}.js可以让您避免每次升级 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.
  • {version}仅适用于路径的最后一部分——基本上是文件名——而不是目录。
  • 同一文件夹中的多个版本的 jquery 都会被追上。

回答by MUG4N

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.

这个包能够在脚本名称中容纳版本号。因此,将 jQuery 更新到应用程序中的新版本(通过 NuGet 或手动)不需要任何代码/标记更改。

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

有关捆绑的更多信息,请参见以下链接: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

回答by leoli

~/Scripts/jquery-{version}.jsis 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.

~/Scripts/jquery-{version}.js包含在其中。当我们在路径中指定 {version} 选择器时,这里的捆绑系统足够智能以引用 jquery 文件的最高版本。此外,这个捆绑系统足够智能,可以选择文件的缩小版本(如果在定义的路径中可用)。