33

ASP.NET 4.5有一個很棒的新綁定功能,並且似乎對使用CDN有一定的支持。微軟使用了CDN的捆綁功能給出的例子是這樣的使用ASP.NET 4.5捆綁和一個CDN(例如,CloudFront)

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文件。

CloudFront CDN(我認爲還有很多其他的)爲您提供了一個反映您自己的子域。當你點擊http://uniquesubdomain.cloudfront.net/js/myfile.js?v=1它提供了http://mydomain.com/js/myfile.js?v=1

這樣你可以簡單地前綴所有鏈接http://uniquesubdomain.cloudfront.net/和你的文件是服務器從CloudFront。

ASP.NET 4.5捆綁功能是否與此類型的CDN兼容?是否有內置的方法可以將綁定功能的所有鏈接添加到您的CDN域?

EG。

bundles.UseCdn = true; 
var myBundle= new ScriptBundle("~/bundles/js", "https://uniquedomain.cloudfront.net/"); 
myBundle.Include("~/js/file1.js"); 
myBundle.Include("~/js/file2.js"); 

會導致

<script src="https://uniquedomain.cloudfront.net/bundles/js?v=6y-qVPSK3RYOYHfPhOBDd92H4LjEjs-D3Hh2Yml6CXA1"></script> 
+2

類似的各種問題http://stackoverflow.com/questions/12047981/how-to-upload-bundled-and-minified-files-to-windows-azure-cdn,just通過更換Azure CDN向您定製cdn – Cris

回答

1

可能不是你要尋找什麼,但很多CDN的現在使用DNS作爲一個反向代理,所以你不必鏈接你的資產明確。我知道Cloudflare這樣做,我確信其他人也這樣做。

+0

我使用SSL,據我所知這不會起作用。 ty爲小費壽。 –

+0

您可以使用SSL和Cloudflare($ 20 /月)。我目前正在電子商務商店使用它。 – orourkedd

+0

我不認爲這將與反向DNS一起工作(除非我誤解某些東西)。如果瀏覽器請求https://yourdomain.com/image。jpg和Cloudflare返回圖像,那麼它將需要*您的* SSL證書,否則該響應將被標記爲不安全的瀏覽器。或者您真的發送Cloudflare您自己的SSL證書,它將用於提供您的內容? –

0

這是不可能的,但是您可以使用文本模板將JS文件合併爲一個js並將其放在CDN上,而不是捆綁。

<#@ ... hostspecific="true" extension=".js"> 

<# 

    Write (System.IO.File.ReadAllText("a.js")); 
    Write (System.IO.File.ReadAllText("b.js")); 
#> 
9

此功能不是內置的,但可以使用一些小的輔助方法。以下是我現在使用什麼:

public static class Cdn 
{ 
    private const string CdnRoot = "//cloudfrontdomainhere.com"; 

    private static bool EnableCdn 
    { 
     get 
     { 
      bool enableCdn = false; 
      bool.TryParse(WebConfigurationManager.AppSettings["EnableCdn"], out enableCdn); 
      return enableCdn; 
     } 
    } 

    public static IHtmlString RenderScripts(string bundlePath) 
    { 
     if (EnableCdn) 
     { 
      string sourceUrl = CdnRoot + Scripts.Url(bundlePath); 
      return new HtmlString(string.Format("<script src=\"{0}\"></script>", sourceUrl)); 
     } 

     return Scripts.Render(bundlePath); 
    } 

    public static IHtmlString RenderStyles(string bundlePath) 
    { 
     if (EnableCdn) 
     { 
      string sourceUrl = CdnRoot + Styles.Url(bundlePath); 
      return new HtmlString(string.Format("<link href=\"{0}\" rel=\"stylesheet\" />", sourceUrl)); 
     } 

     return Styles.Render(bundlePath); 
    } 
} 

請注意,我在我的配置文件的appSettings部分稱爲EnableCdn我自己的配置設置。從Razor視圖調用時,這會產生正確的輸出,將CDN域附加到路徑上。

在你的剃鬚刀的文件只是做Cdn.RenderScripts(「〜/ pathtoscriptbundle」)

+0

一個很好的解決方法。在我的項目中使用它來支持CloudFront –

1

另一種選擇,是使用腳本或樣式RenderFormat方法,像這樣。這對我來說特別有用,因爲我偶爾自定義tagFormat以在條件html註釋中包裝引用,或者追加其他屬性,如media =「screen,print」。代碼比較簡單,因爲在字符串變成HTML編碼的字符串之前,您可以對字符串進行尷尬的替換。

或者,你可以將tagFormat作爲一個可選參數給那些默認值是下面提到的字符串常量的方法。

public class BundleHelper 
{ 
    public static readonly string StyleTagFormat = "<link href=\"{0}\" rel=\"stylesheet\"/>"; 
    public static readonly string ScriptTagFormat = "<script src=\"{0}\"></script>" 

    /// <summary> 
    /// Customised script bundle rendering method with CDN support if optimizations and CDN enabled. 
    /// </summary> 
    public static IHtmlString RenderScriptFormat(string tagFormat, string path) 
    { 
     // Check for absolute url to ensure the standard framework support for CDN bundles, with a CdnPath still works. 
     if (AppSettings.Bundling.EnableCdn && !UriHelper.IsAbsoluteUrl(Scripts.Url(path).ToString())) 
     { 
      tagFormat = tagFormat.Replace(" src=\"{0}\"", String.Format(" src=\"{0}{{0}}\"", AppSettings.Bundling.BundlesCDNPrefixUrl)); 
     } 
     return Scripts.RenderFormat(tagFormat, path); 
    } 

    /// <summary> 
    /// Customised styles bundle rendering method with CDN support if optimizations and CDN enabled. 
    /// </summary> 
    public static IHtmlString RenderStyleFormat(string tagFormat, string path) 
    { 
     // Check for absolute url to ensure the standard framework support for CDN bundles, with a CdnPath still works. 
     if (AppSettings.Bundling.EnableCdn && !UriHelper.IsAbsoluteUrl(Styles.Url(path).ToString())) 
     { 
      tagFormat = tagFormat.Replace(" href=\"{0}\"", String.Format(" href=\"{0}{{0}}\"", AppSettings.Bundling.BundlesCDNPrefixUrl)); 
     } 
     return Styles.RenderFormat(tagFormat, path); 
    } 
} 


public class UriHelper 
{ 
    /// <summary> 
    /// Determines whether a url is absolute or not. 
    /// </summary> 
    /// <param name="url">Url string to test.</param> 
    /// <returns>true/false.</returns> 
    /// <remarks> 
    /// Examples: 
    ///  ?IsAbsoluteUrl("hello") 
    ///  false 
    ///  ?IsAbsoluteUrl("/hello") 
    ///  false 
    ///  ?IsAbsoluteUrl("ftp//hello") 
    ///  false 
    ///  ?IsAbsoluteUrl("//hello") 
    ///  true 
    ///  ?IsAbsoluteUrl("ftp://hello") 
    ///  true 
    ///  ?IsAbsoluteUrl("http://hello") 
    ///  true 
    ///  ?IsAbsoluteUrl("https://hello") 
    ///  true 
    /// </remarks> 
    public static bool IsAbsoluteUrl(string url) 
    { 
     Uri result; 
     return Uri.TryCreate(url, UriKind.Absolute, out result); 
    } 
}