33

ASP.NET 4.5 には優れた新しいバンドル機能があり、CDN の使用をある程度サポートしているようです。CDN とのバンドル機能の使用について Microsoft が示した例は次のとおりです。

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 (および他の多くの 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 ドメインのプレフィックスを付ける組み込みの方法はありますか?

例えば。

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>
4

4 に答える 4

1

もう 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);
    }
}
于 2013-08-29T00:16:39.763 に答える
1

まさにあなたが探しているものではないかもしれませんが、現在多くの CDN は DNS を使用してリバース プロキシとして機能するため、アセットを明示的にリンクする必要はありません。私は Cloudflare がこれを行っていることを知っていますし、他の人もそうしていると確信しています。

于 2013-04-23T17:51:31.767 に答える
0

できませんが、バンドルの代わりにテキスト テンプレートを使用して JS ファイルを 1 つの js として結合し、それを CDN に配置できます。

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

<#

    Write (System.IO.File.ReadAllText("a.js"));
    Write (System.IO.File.ReadAllText("b.js"));
 #>
于 2013-04-26T04:57:51.783 に答える