33

ASP.NET 4.5 का एक नया बेहतरीन बंडलिंग सुविधा है और CDNs के उपयोग के लिए कुछ समर्थन के लिए प्रकट होता का उपयोग करना। उदाहरण के लिए एक 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 के इस प्रकार के साथ संगत है? क्या बंडलिंग सुविधा आपके सीडीएन डोमेन के साथ अपने सभी लिंक उपसर्ग करने का एक अंतर्निहित तरीका है?

ईजी।

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 की इसी तरह से नीला cdn की जगह अपने कस्टम CDN – Cris

उत्तर

1

नहीं हो सकता है आप के लिए वास्तव में क्या देख रहे हैं, लेकिन CDN के का एक बहुत अब तो आप अपनी संपत्ति से जोड़ने के लिए की जरूरत नहीं है DNS का उपयोग एक रिवर्स प्रॉक्सी की तरह कार्य स्पष्ट रूप से। मुझे पता है क्लाउडफ्लारे यह करता है और मुझे यकीन है कि दूसरे भी ऐसा करते हैं।

+0

मैं SSL उपयोग कर रहा हूँ तो जहाँ तक मुझे पता है यह काम नहीं होगा। टिप के लिए ty। –

+0

आप एसएसएल का उपयोग क्लाउडफ्लेयर ($ 20/माह) के साथ कर सकते हैं। मैं वर्तमान में इसे ईकॉमर्स स्टोर पर उपयोग कर रहा हूं। – orourkedd

+0

मुझे नहीं लगता कि यह रिवर्स डीएनएस के साथ काम करेगा हालांकि (जब तक कि मैं कुछ गलत समझ नहीं पा रहा हूं)। यदि ब्राउज़र https://yourdomain.com/image का अनुरोध करता है।जेपीजी और क्लाउडफ्लेयर छवि लौटाता है तो उसे * आपके * एसएसएल प्रमाण की आवश्यकता होगी अन्यथा प्रतिक्रिया ब्राउज़र द्वारा असुरक्षित के रूप में ध्वजांकित की जाएगी। या क्या आप वास्तव में क्लाउडफ्लारे को अपना स्वयं का एसएसएल प्रमाण भेजते हैं जिसका उपयोग आपकी सामग्री की सेवा के लिए किया जाएगा? –

0

यह संभव नहीं है, लेकिन बंडल के बजाय आप एक 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 कहा जाता है अपने ही विन्यास सेटिंग है। जब रेजर व्यू से कॉल किया जाता है, तो यह सही आउटपुट उत्पन्न करता है, जो सीडीएन डोमेन को पथ पर जोड़ता है।

अपने उस्तरा फाइलों में सिर्फ Cdn.RenderScripts कर ("~/pathtoscriptbundle")

+0

एक अच्छा कामकाज। क्लाउडफ्रंट का समर्थन करने के लिए मेरे प्रोजेक्ट में इसका इस्तेमाल किया गया –

1

एक अन्य विकल्प, RenderFormat विधि की तरह तो स्क्रिप्ट या शैलियाँ उपयोग करने के लिए है। के रूप में मैं tagFormat कभी कभी अनुकूलित सशर्त एचटीएमएल टिप्पणियों में संदर्भ लपेट, या मीडिया = "स्क्रीन, प्रिंट" की तरह अतिरिक्त गुण संलग्न करने के लिए यह मेरे लिए विशेष रूप से उपयोगी था। कोड सरल है क्योंकि आप एक HTML-एन्कोडेड स्ट्रिंग बनने से पहले, स्ट्रिंग पर अजीब प्रतिस्थापन कर सकते हैं।

वैकल्पिक रूप से, आप टैगफॉर्मैट को उन विधियों के वैकल्पिक पैरामीटर के रूप में प्राप्त कर सकते हैं जहां डिफ़ॉल्ट मान नीचे वर्णित स्ट्रिंग स्थिरांक है।

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); 
    } 
} 
संबंधित मुद्दे