!!!小心
公认的答案是好的,但如果你有一个高流量的网站,就有可能多次附加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添加v=Hash内容?
编辑:
我试过使用
<%: System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/styles/style.css",true)%>
如果CdUse=true
您不能,将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
更新
您可以通过实现自己的捆绑包将V哈希手动添加到CDN中,并在ApplyTransforms
调用上生成哈希
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();
}