nix*_*xon 43 c# asp.net-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"));
Run Code Online (Sandbox Code Playgroud)
我将其包含在页面中如下:
<!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>
Run Code Online (Sandbox Code Playgroud)
但是当我看到firebug时,似乎从localhost加载了jquery.
我尝试过使用realease和debug构建.我错过了什么?我认为这应该是非常简单的.谢谢.
小智 45
以debug="false"
模式运行您的应用程序或使用BundleTable.EnableOptimizations = true;
juF*_*uFo 16
实际上,当使用最新版本的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;
}
Run Code Online (Sandbox Code Playgroud)
Content Delivery Network(CDN)中可用的jquery版本:http: //www.asp.net/ajax/cdn#jQuery_Releases_on_the_CDN_0
确保您没有处于调试模式.
bundles.Add(new ScriptBundle("~/bundles/jquery", "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js")
Run Code Online (Sandbox Code Playgroud)
设置BundleTable.EnableOptimizations = true;
//如果要使用调试模式
在发布模式下将从CDN请求jQuery,并且将在调试模式下本地获取jQuery的调试版本.使用CDN时,如果CDN请求失败,您应该有一个回退机制.
如果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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
60641 次 |
最近记录: |