C# 在 MVC 脚本包中使用 CDN。我错过了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15401387/
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
Using CDN in MVC script bundle. What am I missing?
提问by nixon
I am trying to use a CDN for loading jquery. I have read thisarticle and this seems like it should be very straightforward.
我正在尝试使用 CDN 来加载 jquery。我已经阅读了这篇文章,这看起来应该很简单。
My script bundle is defined as follows.
我的脚本包定义如下。
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"));
I am including it on the page as follows:
我将它包含在页面上,如下所示:
<!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>
But when I look at firebug it seems that jquery is being loaded from localhost.
但是当我查看 firebug 时,似乎 jquery 是从本地主机加载的。
I have tried with both realease and debug builds. What am I missing? I think this should be quite straightforward. Thanks.
我已经尝试过 realease 和 debug 版本。我错过了什么?我认为这应该很简单。谢谢。
采纳答案by imran_ku07
Run your application in debug="false"
mode or use BundleTable.EnableOptimizations = true;
以debug="false"
模式运行您的应用程序或使用BundleTable.EnableOptimizations = true;
回答by Ravi Gadag
make sure you are not in debug mode.
确保您未处于调试模式。
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js")
set BundleTable.EnableOptimizations = true;
// if you want to use debug mode
set BundleTable.EnableOptimizations = true;
// 如果你想使用调试模式
jQuery will be requested from the CDN while in release mode and the debug version of jQuery will be fetched locally in debug mode. When using a CDN, you should have a fallback mechanism in case the CDN request fails.
jQuery 将在发布模式下从 CDN 请求,jQuery 的调试版本将在调试模式下在本地获取。使用 CDN 时,您应该有一个回退机制,以防 CDN 请求失败。
if CDN Request fail then you can provide a callback
如果 CDN 请求失败,则您可以提供回调
<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>
回答by juFo
Actually you can write @RaviGadag his method shorter when using a recent version of ASP.NET MVC. This way you don't have to write the fallback yourself in the Layout:
实际上,当使用最新版本的 ASP.NET MVC 时,您可以更短地编写 @RaviGadag 他的方法。这样您就不必在布局中自己编写回退:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.UseCdn = true;
var jqueryCdnPath = "http://ajax.aspnetcdn.com/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;
}
available jquery versions in the Content Delivery Network (CDN): http://www.asp.net/ajax/cdn#jQuery_Releases_on_the_CDN_0
内容交付网络 (CDN) 中可用的 jquery 版本:http: //www.asp.net/ajax/cdn#jQuery_Releases_on_the_CDN_0