【问题标题】:Using ASP.NET 4.5 Bundling & a CDN (eg. CloudFront)使用 ASP.NET 4.5 捆绑和 CDN(例如 CloudFront)
【发布时间】:2012-11-02 19:18:05
【问题描述】:

ASP.NET 4.5 有一个很棒的新捆绑功能,并且似乎支持使用 CDN。 Microsoft 给出的使用 CDN 捆绑功能的示例是这样的

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 域的链接的前缀?

例如。

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>

【问题讨论】:

标签: c# amazon-cloudfront asp.net-4.5 bundling-and-minification


【解决方案1】:

此功能不是内置的,但可以通过几个小帮助方法实现。这是我现在使用的:

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 域附加到路径上。

在您的 Razor 文件中,只需执行 Cdn.RenderScripts("~/pathtoscriptbundle")

【讨论】:

  • 一个很好的解决方法。在我的项目中使用它来支持 CloudFront
【解决方案2】:

可能不是您正在寻找的内容,但许多 CDN 现在使用 DNS 充当反向代理,因此您不必明确链接您的资产。我知道 Cloudflare 会这样做,我相信其他人也会这样做。

【讨论】:

  • 据我所知,我正在使用 SSL,这不起作用。 ty 给小费。
  • 您可以将 SSL 与 cloudflare 一起使用(20 美元/月)。我目前正在电子商务商店中使用它。
  • 我认为这不适用于反向 DNS(除非我误解了某些东西)。如果浏览器请求 yourdomain.com/image.jpg 并且 Cloudflare 返回图像,那么它将需要 your SSL 证书,否则浏览器会将响应标记为不安全。还是您实际上向 Cloudflare 发送了您自己的 SSL 证书,它将用于为您的内容提供服务?
  • Cloudflare 为您生成一个证书,用于从其服务器提取数据。他们很聪明。
  • SSL 现在甚至可用于 Cloudflare 上的免费计划。
【解决方案3】:

另一种选择是像这样使用 Scripts 或 Styles RenderFormat 方法。这对我特别有用,因为我偶尔自定义 tagFormat 以将引用包装在条件 html cmets 中,或附加其他属性,如 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);
    }
}

【讨论】:

    【解决方案4】:

    这是不可能的,但是你可以使用文本模板将 JS 文件合并为一个 js 并将其放在 CDN 上,而不是 bundle。

    <#@ ... hostspecific="true"  extension=".js">
    
    <#
    
        Write (System.IO.File.ReadAllText("a.js"));
        Write (System.IO.File.ReadAllText("b.js"));
     #>
    

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 2023-03-21
      • 2013-03-09
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 2021-08-31
      • 2017-09-20
      • 2013-09-11
      相关资源
      最近更新 更多