5

我使用asp.net捆綁/縮小,並把一切都在bundle.config這樣的:如何添加CDN在asp.net web表單來bundle.config捆綁

<styleBundle path="~/css/css"> 
    <include path="~/css/bootstrap.css" /> 
    <include path="~/css/flexslider.css" /> 
    <include path="~/css/font-awesome.css" /> 
    <include path="~/css/Site.css" /> 
    <include path="~/css/orange.css" /> 
</styleBundle> 

但我想用引導的CSS從CDN:

//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css 

那麼我們怎樣才能做到這一點的bundle.config?

回答

4

目前不能混搭,以及像CDN外部源拉的一些文件在你的包。您可以將整個軟件包上傳到CDN並配置助手在CDN中呈現對該軟件包的引用,但不能包含來自外部源的文件,這些文件必須存在於您的應用可以找到的某個位置。

您可以解決此通過實施的VirtualPathProvider,這是能夠在運行時從您的CDN獲取文件,但你必須要建立一個自己。

0

不能混合捆綁,但您可以在您的boundle配置外部來源。

這裏上課從here選爲randomidea指出的例子。

public static void RegisterBundles(BundleCollection bundles) 
{ 
    //bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
    //   "~/Scripts/jquery-{version}.js")); 

    bundles.UseCdn = true; //enable CDN support 

    //add link to jquery on the CDN 
    var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"; 

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

// Code removed for clarity. 
} 

我們需要啓用CDN,這樣做我們設置UseCdn爲true,我們在ScriptBundle構造函數添加URL。包含文件將在調試模式下使用。

正如文章建議,我們需要在情況下,我們失敗CDN一個回退機制:

@Scripts.Render("~/bundles/jquery") 

    <script type="text/javascript"> 
     if (typeof jQuery == 'undefined') { 
      var e = document.createElement('script'); 
      e.src = '@Url.Content("~/Scripts/jquery-1.7.1.js")'; 
      e.type = 'text/javascript'; 
      document.getElementsByTagName("head")[0].appendChild(e); 

     } 
    </script> 

希望這有助於。