简体   繁体   English

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

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

I´m trying to add some CDN capable bundle with ASP.NET MVC 4. The purpose is to share content locally by many other sites hosted in the same data center 我试图在ASP.NET MVC 4中添加一些支持CDN的捆绑包。目的是在同一数据中心托管的许多其他站点在本地共享内容

The first attempt was: 第一次尝试是:

            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"));

Unfortunatelly, this is not possible, because the virtualPaths must be relative (Only application relative URLs (~/url) are allowed) 不幸的是,这是不可能的,因为virtualPath必须是相对的(只允许应用程序相对URL(〜/ url))

Then I´ve tried this: 然后我试过这个:

        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"));

But it hasn't worked, even enabling CDN: 但它没有奏效,即使启用CDN:

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

Is it possible to create a multiple content bundle with CDN? 是否可以使用CDN创建多个内容包?

AFAIK you can't not serve multiple CDN hosts in one bundle. AFAIK您不能在一个捆绑包中提供多个CDN主机。 ScriptBundle allows you to specify an alternate URL for the bundle and the bundle could contain several local files. ScriptBundle允许您为捆绑包指定备用URL,捆绑包可以包含多个本地文件。 The syntax you have is correct. 你的语法是正确的。

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

There are a couple of ways to solve this problem. 有几种方法可以解决这个问题。

  1. Have one bundle per CDN hosted script. 每个CDN托管脚本都有一个捆绑包。
  2. Manually create a bundle of the files and upload them to your own CDN and reference that. 手动创建一个文件包并将它们上传到您自己的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