I read the article about bundling and monification, specially about using CDN, but there are some things unclear to me.
我阅读了有关捆绑和通知的文章,特别是有关使用CDN的文章,但有一些事情我不清楚。
Having the example :
有这样的例子:
public static void RegisterBundles(BundleCollection bundles)
{
//bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
// "~/Scripts/jquery-{version}.js"));
bundles.UseCdn = true; //enable CDN support
//add link to jquery on the CDN
var jqueryCdnPath =
"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";
bundles.Add(new ScriptBundle("~/bundles/jquery",
jqueryCdnPath).Include(
"~/Scripts/jquery-{version}.js"));
// Code removed for clarity.
}
-
Is there a possibility to use the
{version}
format of CDN references, like for the "local" ones?是否有可能使用{version}格式的CDN引用,比如“本地”引用?
-
What is the point of including in the bundles the already minified version of the script, like jquery-1.7.1.min.js? What if it does not exist? Should it not search if the
.min
file exist and/or generate it respectively?在捆绑包中包含已经缩小的脚本版本(如jquery-1.7.1.min.js)有什么意义?如果它不存在怎么办?它是否应该搜索.min文件是否存在和/或分别生成它?
3 个解决方案
#1
2
using System.Web;
using System.Web.Optimization;
namespace MvcApp
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js").Include("~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap","https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js").Include("~/Scripts/bootstrap.js"));
bundles.Add(new StyleBundle("~/Content/css", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css").Include("~/Content/bootstrap.css"));
BundleTable.EnableOptimizations = true;
bundles.UseCdn = true;
}
}
}
What a lot of developers don't realized is that there is an overload for class constructor of ScriptBundle and StyleBundle, which takes two string parameters, for example for the ScriptBundle it would be ScriptBundle(string, string) and for the StyleBundle it would be StyleBundle(string, string). The first parameter is the virtual path and the second parameter is the cdnPath.
许多开发人员没有意识到,ScriptBundle和StyleBundle的类构造函数有一个重载,它带有两个字符串参数,例如ScriptBundle它将是ScriptBundle(字符串,字符串),而对于StyleBundle它将是StyleBundle(字符串,字符串)。第一个参数是虚拟路径,第二个参数是cdnPath。
We might be asking yourself, if it takes two parameters, how does MVC know which one to use? Well, the cdn location is used only when the BundleTable.EnableOptimizations property is set to true.
我们可能会问自己,如果需要两个参数,MVC如何知道使用哪一个?好吧,仅当BundleTable.EnableOptimizations属性设置为true时才使用cdn位置。
Setting the EnableOptimization property to true tells MVC to use the use the minified version of the file instead of the regular version.
将EnableOptimization属性设置为true会告诉MVC使用文件的缩小版本而不是常规版本。
When this property is set to true, and the cdn path is present MVC will use the cdn path instead of the local virtual path.
There is one more property you have to set to true and that is the bundles.UseCdn.
This tells MVC to use the cdn location instead of the local version. If the BundleTable.EnableOptimization is set to false, then the local version is used automatically as a fall back because the cdn version is the minified version.
当此属性设置为true并且cdn路径存在时,MVC将使用cdn路径而不是本地虚拟路径。还有一个属性你需要设置为true,那就是bundles.UseCdn。这告诉MVC使用cdn位置而不是本地版本。如果BundleTable.EnableOptimization设置为false,则本地版本将自动用作回退,因为cdn版本是缩小版本。
Read this blog its clear about your think:
阅读本博客,清楚地了解您的想法:
http://www.techjunkieblog.com/2015/06/aspnet-mvc-5-configure-bundleconfig.html
http://www.techjunkieblog.com/2015/06/aspnet-mvc-5-configure-bundleconfig.html
#2
0
-
You can't to my knowledge. But you can keep a table of cdns and populate when the bundles are loaded. When a new version comes out you wish to use, add/replace entry in the db.
你不能以我的知识。但是你可以保存一张cdns表并在加载bundle时填充。当您希望使用新版本时,在db中添加/替换条目。
//get from db List<string> cdns = new List<string>(); foreach (string cdn in cdns) { bundles.Add(new ScriptBundle("~/bundles/jquery",cdn).Include("~/Scripts/jquery-{version}.js")); }
-
I agree on the min part. For the doesn't exist part of the question, scroll down and read about "Using a CDN". There's an example to show how to check. You essentially need to have a local copy as backup of you can reference another cdn I suppose.
我同意最小的部分。对于问题不存在的部分,请向下滚动并阅读“使用CDN”。有一个例子来说明如何检查。你基本上需要有一个本地副本作为备份,你可以引用另一个cdn我想。
#3
0
Is there a possibility to use the {version} format of CDN references, like for the "local" ones?
是否有可能使用{version}格式的CDN引用,比如“本地”引用?
The {version} placeholder is primarily for saving time typing the explicit number so that the build could look for files on local disk. Since the same search could not be done on a remote server, you will need to specify an exact URL explicitly.
{version}占位符主要用于节省键入显式数字的时间,以便构建可以在本地磁盘上查找文件。由于无法在远程服务器上执行相同的搜索,因此您需要明确指定确切的URL。
What is the point of including in the bundles the already minified version of the script, like jquery-1.7.1.min.js? What if it does not exist?
在捆绑包中包含已经缩小的脚本版本(如jquery-1.7.1.min.js)有什么意义?如果它不存在怎么办?
The key benefit to go with this bundling syntax is to conditionally switch between different URLs for script and style tags in the final HTML.
使用此捆绑语法的主要好处是在最终HTML中有条件地在脚本和样式标记的不同URL之间切换。
When a requested file does not exist, the bundling process will skip it.
当请求的文件不存在时,捆绑过程将跳过它。
Should it not search if the .min file exist and/or generate it respectively? Yes, it applies minification before bundling as you can see:
它是否应该搜索.min文件是否存在和/或分别生成它?是的,它可以在捆绑之前应用缩小,如您所见:
#1
2
using System.Web;
using System.Web.Optimization;
namespace MvcApp
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js").Include("~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/bootstrap","https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js").Include("~/Scripts/bootstrap.js"));
bundles.Add(new StyleBundle("~/Content/css", "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css").Include("~/Content/bootstrap.css"));
BundleTable.EnableOptimizations = true;
bundles.UseCdn = true;
}
}
}
What a lot of developers don't realized is that there is an overload for class constructor of ScriptBundle and StyleBundle, which takes two string parameters, for example for the ScriptBundle it would be ScriptBundle(string, string) and for the StyleBundle it would be StyleBundle(string, string). The first parameter is the virtual path and the second parameter is the cdnPath.
许多开发人员没有意识到,ScriptBundle和StyleBundle的类构造函数有一个重载,它带有两个字符串参数,例如ScriptBundle它将是ScriptBundle(字符串,字符串),而对于StyleBundle它将是StyleBundle(字符串,字符串)。第一个参数是虚拟路径,第二个参数是cdnPath。
We might be asking yourself, if it takes two parameters, how does MVC know which one to use? Well, the cdn location is used only when the BundleTable.EnableOptimizations property is set to true.
我们可能会问自己,如果需要两个参数,MVC如何知道使用哪一个?好吧,仅当BundleTable.EnableOptimizations属性设置为true时才使用cdn位置。
Setting the EnableOptimization property to true tells MVC to use the use the minified version of the file instead of the regular version.
将EnableOptimization属性设置为true会告诉MVC使用文件的缩小版本而不是常规版本。
When this property is set to true, and the cdn path is present MVC will use the cdn path instead of the local virtual path.
There is one more property you have to set to true and that is the bundles.UseCdn.
This tells MVC to use the cdn location instead of the local version. If the BundleTable.EnableOptimization is set to false, then the local version is used automatically as a fall back because the cdn version is the minified version.
当此属性设置为true并且cdn路径存在时,MVC将使用cdn路径而不是本地虚拟路径。还有一个属性你需要设置为true,那就是bundles.UseCdn。这告诉MVC使用cdn位置而不是本地版本。如果BundleTable.EnableOptimization设置为false,则本地版本将自动用作回退,因为cdn版本是缩小版本。
Read this blog its clear about your think:
阅读本博客,清楚地了解您的想法:
http://www.techjunkieblog.com/2015/06/aspnet-mvc-5-configure-bundleconfig.html
http://www.techjunkieblog.com/2015/06/aspnet-mvc-5-configure-bundleconfig.html
#2
0
-
You can't to my knowledge. But you can keep a table of cdns and populate when the bundles are loaded. When a new version comes out you wish to use, add/replace entry in the db.
你不能以我的知识。但是你可以保存一张cdns表并在加载bundle时填充。当您希望使用新版本时,在db中添加/替换条目。
//get from db List<string> cdns = new List<string>(); foreach (string cdn in cdns) { bundles.Add(new ScriptBundle("~/bundles/jquery",cdn).Include("~/Scripts/jquery-{version}.js")); }
-
I agree on the min part. For the doesn't exist part of the question, scroll down and read about "Using a CDN". There's an example to show how to check. You essentially need to have a local copy as backup of you can reference another cdn I suppose.
我同意最小的部分。对于问题不存在的部分,请向下滚动并阅读“使用CDN”。有一个例子来说明如何检查。你基本上需要有一个本地副本作为备份,你可以引用另一个cdn我想。
#3
0
Is there a possibility to use the {version} format of CDN references, like for the "local" ones?
是否有可能使用{version}格式的CDN引用,比如“本地”引用?
The {version} placeholder is primarily for saving time typing the explicit number so that the build could look for files on local disk. Since the same search could not be done on a remote server, you will need to specify an exact URL explicitly.
{version}占位符主要用于节省键入显式数字的时间,以便构建可以在本地磁盘上查找文件。由于无法在远程服务器上执行相同的搜索,因此您需要明确指定确切的URL。
What is the point of including in the bundles the already minified version of the script, like jquery-1.7.1.min.js? What if it does not exist?
在捆绑包中包含已经缩小的脚本版本(如jquery-1.7.1.min.js)有什么意义?如果它不存在怎么办?
The key benefit to go with this bundling syntax is to conditionally switch between different URLs for script and style tags in the final HTML.
使用此捆绑语法的主要好处是在最终HTML中有条件地在脚本和样式标记的不同URL之间切换。
When a requested file does not exist, the bundling process will skip it.
当请求的文件不存在时,捆绑过程将跳过它。
Should it not search if the .min file exist and/or generate it respectively? Yes, it applies minification before bundling as you can see:
它是否应该搜索.min文件是否存在和/或分别生成它?是的,它可以在捆绑之前应用缩小,如您所见: