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> 

希望这有助于。