使用CDN和HashContent(Cache Buster或指纹)的ASP.NET优化框架(JS和CSS缩小和捆绑)

时间:2014-04-17 23:18:43

标签: asp.net asp.net-mvc optimization bundling-and-minification

!!!小心!!!

接受的答案很好,但如果你有一个高流量的网站,则有可能多次附加v =。该代码包含一个检查器。

我一直在寻找使用ASP.NET优化框架的任何示例或引用,其中UseCDN = true,而HashContent Number附加到Bundles的URI。不幸没有运气。以下是我的代码的简化示例。

我的捆绑代码很简单

        bundles.UseCdn = true;

        BundleTable.EnableOptimizations = true;


        var stylesCdnPath = "http://myCDN.com/style.css";
        bundles.Add(new StyleBundle("~/bundles/styles/style.css", stylesCdnPath).Include(
            "~/css/style.css"));

我从主页面调用渲染

 <%: System.Web.Optimization.Styles.Render("~/bundles/styles/style.css")%>

生成的代码是

 <link href="http://myCDN.com/style.css" rel="stylesheet"/>

如果我禁用UseCDN

 /bundles/styles/style.css?v=geCEcmf_QJDXOCkNczldjY2sxsEkzeVfPt_cGlSh4dg1

当useCDN设置为true时,如何使bunlding add v = Hash Content?

编辑:

我尝试使用

 <%: System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/styles/style.css",true)%> 

如果CdnUse = true

,它仍然不会生成v = hash

1 个答案:

答案 0 :(得分:8)

您不能将UseCdn设置为true表示ASP.NET将按照您的CDN路径提供捆绑包,不会执行捆绑和缩小。

  

查询字符串v具有值标记,该标记是使用的唯一标识符   用于缓存。只要捆绑包没有改变,ASP.NET就可以了   应用程序将使用此令牌请求捆绑包。如果有任何文件   捆绑包更改后,ASP.NET优化框架将生成一个   新令牌,保证浏览器对捆绑包的请求得到   最新的捆绑。

查看BundleCollection.ResolveBundleUrl实施:

// System.Web.Optimization.BundleCollection
/// <summary>Returns the bundle URL for the specified virtual path, including a content hash if requested.</summary>
/// <returns>The bundle URL or null if the bundle cannot be found.</returns>
/// <param name="bundleVirtualPath">The virtual path of the bundle.</param>
/// <param name="includeContentHash">true to include a hash code for the content; otherwise, false. The default is true.</param>
public string ResolveBundleUrl(string bundleVirtualPath, bool includeContentHash)
{
    Exception ex = ExceptionUtil.ValidateVirtualPath(bundleVirtualPath, "bundleVirtualPath");
    if (ex != null)
    {
        throw ex;
    }
    Bundle bundleFor = this.GetBundleFor(bundleVirtualPath);
    if (bundleFor == null)
    {
        return null;
    }
    if (this.UseCdn && !string.IsNullOrEmpty(bundleFor.CdnPath))
    {
        return bundleFor.CdnPath;
    }
    return bundleFor.GetBundleUrl(new BundleContext(this.Context, this, bundleVirtualPath), includeContentHash);
}

参考:http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

<强>更新

您可以通过实施自己的捆绑包并在ApplyTransforms调用上生成哈希值,将V哈希手动添加到您的CDN中:

public class myStyleBundle: StyleBundle
{
  public myStyleBundle(string virtualPath)
    :base(virtualPath)
  {          
  }

  public myStyleBundle(string virtualPath, string cdnPath)
    : base(virtualPath,cdnPath)
  {
    MyCdnPath = cdnPath;
  }

  public string MyCdnPath
  {
    get;
    set;
  }

  public override BundleResponse ApplyTransforms(BundleContext context, string bundleContent, System.Collections.Generic.IEnumerable<BundleFile> bundleFiles)
  {
    var response = base.ApplyTransforms(context, bundleContent, bundleFiles);

    base.CdnPath = string.Format("{0}?v={1}", this.MyCdnPath, this.HashContent(response));

    return response;
  }

  private string HashContent(BundleResponse response)
  {
    string result;
    using (SHA256 sHA = new SHA256Managed())
    {
      byte[] input2 = sHA.ComputeHash(Encoding.Unicode.GetBytes(response.Content));
      result = HttpServerUtility.UrlTokenEncode(input2);
    }
    return result;
  }

}

然后,只需:

bundles.Add(new myStyleBundle("~/bundles/styles/style.css", stylesCdnPath).Include(
           "~/css/style.css"));

请注意System.Web.Optimization.BundleResponse根据环境设置创建哈希算法:

// System.Web.Optimization.BundleResponse
private static SHA256 CreateHashAlgorithm()
{
  if (BundleResponse.AllowOnlyFipsAlgorithms)
  {
     return new SHA256CryptoServiceProvider();
  }
  return new SHA256Managed();
}