4

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 で複数のコンテンツ バンドルを作成することはできますか?

4

2 に答える 2

1

AFAIK you can't not serve multiple CDN hosts in one bundle. ScriptBundle allows you to specify an alternate URL for the bundle and the bundle could contain several local files. 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.
  2. Manually create a bundle of the files and upload them to your own CDN and reference that.
于 2015-06-26T09:27:08.107 に答える
0
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"));
}
于 2015-06-25T22:14:28.797 に答える