33

ASP.NET 4.5 ha una nuova fantastica funzione di raggruppamento e sembra avere un certo supporto per l'uso di CDN. L'esempio dato da Microsoft per l'utilizzo della funzione di raggruppamento con un CDN è questoUtilizzo di Bundle ASP.NET 4.5 e un CDN (ad esempio 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. 
} 

che sembra suggerire che è necessario dirlo esplicitamente il percorso del file sul CDN.

Il CDN di CloudFront (e ne presumo molti altri) offre un sottodominio che rispecchia il proprio. Quando si preme http://uniquesubdomain.cloudfront.net/js/myfile.js?v=1 serve fino http://mydomain.com/js/myfile.js?v=1

In questo modo è possibile semplicemente aggiungere un prefisso a tutti i collegamenti con http://uniquesubdomain.cloudfront.net/ ei file sono server di CloudFront.

La funzionalità di raggruppamento ASP.NET 4.5 è compatibile con questo tipo di CDN? Esiste un modo integrato per far precedere dalla funzionalità di raggruppamento tutti i suoi collegamenti con il tuo dominio CDN?

Es.

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

causerebbe

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

simile tipo di domanda http://stackoverflow.com/questions/12047981/how-to-upload-bundled-and-minified-files-to-windows-azure-cdn,just sostituire CDN azzurro dalla vostra cdn personalizzato – Cris

risposta

1

Potrebbe non essere esattamente quello che stai cercando, ma un sacco di CDN di ora agire come un proxy inverso utilizzando DNS in modo da non dover collegare il vostro patrimonio in modo esplicito. So che Cloudflare fa questo e sono sicuro che anche gli altri lo facciano.

+0

Sto usando SSL, per quanto ne so, questo non funzionerà. per la punta. –

+0

È possibile utilizzare SSL con cloudflare ($ 20/mese). Attualmente lo sto usando su un negozio di ecommerce. – orourkedd

+0

Non penso che funzionerà con il DNS inverso (a meno che non stia fraintendendo qualcosa). Se il browser richiede https://yourdomain.com/image.jpg e Cloudflare restituiscono l'immagine, quindi avrà bisogno del tuo * cert SSL * altrimenti la risposta verrà contrassegnata come non sicura dal browser. Oppure invii effettivamente Cloudflare al tuo certificato SSL che utilizzerà per pubblicare i tuoi contenuti? –

0

Non è possibile, ma invece di un pacchetto è possibile utilizzare un modello di testo per combinare i file JS come uno js e inserirli su CDN.

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

<# 

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

Questa funzionalità non è integrata, ma è possibile con un paio di metodi di supporto di piccole dimensioni. Ecco quello che sto usando in questo momento:

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); 
    } 
} 

Si noti che io ho la mia impostazione di configurazione chiamato EnableCdn nella sezione appSettings del mio file di configurazione. Quando viene chiamato da una vista Razor, questo produce l'output corretto, che aggiunge il dominio CDN ai percorsi.

nei file Razor solo fare Cdn.RenderScripts ("~/pathtoscriptbundle")

+0

Una buona soluzione. Utilizzato questo nel mio progetto per supportare CloudFront –

1

Un'altra opzione, è quello di utilizzare gli script o Stili RenderFormat metodo in questo modo. Questo è stato particolarmente utile per me in quanto personalizzo il tagFormat occasionalmente per includere i riferimenti nei commenti html condizionali o aggiungere attributi aggiuntivi come media = "screen, print". Il codice è più semplice perché puoi eseguire il comando Sostituisci su una stringa, prima che diventi una stringa con codifica HTML.

In alternativa, è possibile che tagFormat sia un parametro facoltativo per quei metodi in cui il valore predefinito è le costanti di stringa menzionate di seguito.

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); 
    } 
} 
Problemi correlati