繁体   English   中英

ASP.NET MVC多个虚拟路径捆绑带CDN

[英]ASP.NET MVC multiple virtualpath Bundle with CDN

我试图在ASP.NET MVC 4中添加一些支持CDN的捆绑包。目的是在同一数据中心托管的许多其他站点在本地共享内容

第一次尝试是:

            bundles.Add(new ScriptBundle("~/bundles/jquery", "http://mysite/Content/js/").Include(
                                                              "http://mycdnsite/Content/js/jquery.unobtrusive-ajax.min.js",
                                                              "http://mycdnsite/Content/js/jquery-migrate-1.2.1.js",
                                                              "http://mycdnsite/Content/js/jquery-{version}.js"));

不幸的是,这是不可能的,因为virtualPath必须是相对的(只允许应用程序相对URL(〜/ url))

然后我试过这个:

        bundles.Add(new ScriptBundle("~/bundles/jquery", "http://mycdnsite/Content/js/").Include(
                                                              "~/jquery.unobtrusive-ajax.min.js",
                                                              "~/jquery-migrate-1.2.1.js",
                                                              "~/jquery-{version}.js"));

但它没有奏效,即使启用CDN:

BundleTable.EnableOptimizations = true;
bundles.UseCdn = true;

是否可以使用CDN创建多个内容包?

AFAIK您不能在一个捆绑包中提供多个CDN主机。 ScriptBundle允许您为捆绑包指定备用URL,捆绑包可以包含多个本地文件。 你的语法是正确的。

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

有几种方法可以解决这个问题。

  1. 每个CDN托管脚本都有一个捆绑包。
  2. 手动创建一个文件包并将它们上传到您自己的CDN并引用它。
public static void RegisterBundles(BundleCollection bundles)
{
    bundles.UseCdn = true;   // enable CDN     
    // How to add link to jQuery on the CDN ?
    var jqueryCdnPath = "http://mycdnsite/Content/js/jquery.unobtrusive-ajax.min.js";

    bundles.Add(new ScriptBundle("~/bundles/jquery", jqueryCdnPath)
           .Include("~/Scripts/jquery-{version}.js"));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM