Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CDN和HashContent(缓存Buster或指纹)的ASP.NET优化框架(JS和CSS缩小和绑定) !!! 小心 被接受的答案是好的,但是如果你有一个高流量的网站,就有可能附加v=多次。代码包含一个检查器。_Asp.net_Asp.net Mvc_Optimization_Bundling And Minification - Fatal编程技术网

使用CDN和HashContent(缓存Buster或指纹)的ASP.NET优化框架(JS和CSS缩小和绑定) !!! 小心 被接受的答案是好的,但是如果你有一个高流量的网站,就有可能附加v=多次。代码包含一个检查器。

使用CDN和HashContent(缓存Buster或指纹)的ASP.NET优化框架(JS和CSS缩小和绑定) !!! 小心 被接受的答案是好的,但是如果你有一个高流量的网站,就有可能附加v=多次。代码包含一个检查器。,asp.net,asp.net-mvc,optimization,bundling-and-minification,Asp.net,Asp.net Mvc,Optimization,Bundling And Minification,我一直在寻找ASP.NET优化框架与UseCDN=true一起使用并且HashContent Number附加到bundle的URI的任何示例或参考。不幸的是没有任何运气。下面是我的代码的简化示例 我的捆绑代码非常简单 bundles.UseCdn = true; BundleTable.EnableOptimizations = true; var stylesCdnPath = "http://myCDN.com/style.css";

我一直在寻找ASP.NET优化框架与UseCDN=true一起使用并且HashContent Number附加到bundle的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")%>
当useCDN设置为true时,如何使bunlding add v=Hash内容

编辑:

我试着用

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


如果CdnUse=true

您不能,它仍然不会生成v=hash,将
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);
}
然后,简单地做:

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

尝试在发行版中运行应用程序configuration@BhushanFirake在发行版中运行时没有添加v=…@rommik。代码使用
MyCdnPath
更新
CdnPath
,其中后面的
MyCdnPath
是不应该包含哈希的实际CDN路径。如果按原样使用,则代码正常!您好@MK。谢谢您的回复。很抱歉,您所说的“将UseCdn设置为true意味着ASP.NET将从您的CDN路径按原样提供捆绑包,不会执行捆绑和缩小”是错误的。捆绑和缩小仍在执行。但是,渲染脚本和样式的路径是CDN路径,而不是服务器的路径。您仍然可以浏览到服务器上的捆绑包,它们将被缩小。您的代码是正确的,但这是ResolveBundleUrl的代码,在执行缩小和绑定之前(在应用程序启动时)。@rommik抱歉,如果我不清楚,我的意思是:CDN内容不会被ASP.NET捆绑或缩小,它将按原样输出URL。将V哈希添加到CDN路径是无用的。你为什么一开始就想要它?不管怎样,回答更新了。谢谢@你提供的解决方案正是我一直在寻找的。我需要V hash的原因是破坏用户的缓存并强制他们的浏览器更新脚本/样式。这还强制我的CDN刷新其缓存,并从我的服务器请求新版本。我感到惊讶的是,这一选择没有在框架中实施。似乎很多网站都使用带有CDN的CacheBuster。
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
private static SHA256 CreateHashAlgorithm()
{
  if (BundleResponse.AllowOnlyFipsAlgorithms)
  {
     return new SHA256CryptoServiceProvider();
  }
  return new SHA256Managed();
}