在MVC脚本包中使用CDN。 我错过了什么?

我正在尝试使用CDN加载jQuery。 我已阅读这篇文章,这似乎应该是非常简单的。

我的脚本包定义如下。

bundles.UseCdn = true; bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js").Include( "~/Scripts/jquery-{version}.js")); 

我将其包含在页面上如下:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> @RenderBody() @Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false) </body> </html> 

但是当我看着萤火虫似乎jquery正在从本地主机加载。 萤火虫检查

我已经尝试使用realease和debug builds /。 我错过了什么? 我认为这应该是相当简单的。 谢谢。

debug="false"模式下运行你的应用程序,或者使用BundleTable.EnableOptimizations = true;

其实你可以在使用最新版本的ASP.NET MVC的时候写@RaviGadag他的方法。 这样您就不必在布局中自己编写回退:

 public static void RegisterBundles(BundleCollection bundles) { bundles.UseCdn = true; var jqueryCdnPath = "ajax/jQuery/jquery-2.1.3.min.js"; var jqueryBundle = new ScriptBundle("~/bundles/jquery", jqueryCdnPath).Include("~/Scripts/jquery-{version}.min.js"); jqueryBundle.CdnFallbackExpression = "window.jQuery"; bundles.Add(jqueryBundle); // ... BundleTable.EnableOptimizations = true; } 

内容交付networking(CDN)中的可用jQuery版本: http : //www.asp.net/ajax/cdn#jQuery_Releases_on_the_CDN_0

确保你不在debugging模式下。

  bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js") 

设置BundleTable.EnableOptimizations = true; //如果你想使用debugging模式

在发布模式下jQuery将被请求从CDN中,jQuery的debugging版本将在debugging模式下本地获取。 在使用CDN时,如果CDN请求失败,应该有一个回退机制。

如果CDN请求失败,则可以提供callback

 <script type="text/javascript"> if (typeof jQuery == 'undefined') { var e = document.createElement('script'); e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")'; e.type = 'text/javascript'; document.getElementsByTagName("head")[0].appendChild(e); } </script>