【问题标题】:ASP.NET MVC multiple virtualpath Bundle with CDNASP.NET MVC 多虚拟路径捆绑与 CDN
【发布时间】:2015-09-12 16:57:38
【问题描述】:

我正在尝试使用 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"));

不幸的是,这是不可能的,因为 virtualPaths 必须是相对的 (仅允许应用相对 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 创建多个内容包?

【问题讨论】:

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


    【解决方案1】:
    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"));
    }
    

    【讨论】:

    • 当我添加一个虚拟路径时,这很好用。我想要做的是将 ~/bundles/jquery 映射到许多虚拟路径...
    【解决方案2】:

    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 并引用它。

    【讨论】:

    • 谢谢科林! “合并”选项会解决,但这不是理想的解决方案。我正在尝试找出其他方法来远程托管这个多个脚本并由 System.Optimization(或其他一些包)引用
    猜你喜欢
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多