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

时间:2015-06-25 21:43:46

标签: c# asp.net asp.net-mvc-4 cdn bundles

我正在尝试使用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创建多个内容包?

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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"));
}