Bha*_*ava 5 c# asp.net bundles asp.net-mvc-4
我们有一个MVS应用程序,我们使用Bundle类捆绑javascript代码(不要做缩小).
捆绑工作正常,但是当我们运行应用程序时,缓存值设置为Cache-Control:no-cache,同时每次刷新页面时请求总是有200 OK.这意味着即使没有更改,js也不会缓存在客户端上.
还有一种方法可以验证捆绑的js是动态构建还是从服务器缓存中获取?
谢谢
我看到了与codeplex链接中描述的相同的行为,提到了这个问题:
即如果我按以下顺序访问这些URL,则行为是 -
Run Code Online (Sandbox Code Playgroud)bundle.css?v=1234 : no-cache bundle.css : public bundle.css?v=1234 : public
我决定深入研究一下System.Web.Optimization源代码,看看发生了什么.在Bundle类上,有一个私有方法设置标题,它似乎属于这段代码:
if (noCache) {
cachePolicy.SetCacheability(HttpCacheability.NoCache);
}
Run Code Online (Sandbox Code Playgroud)
noCache变量通过参数设置.这种情况下的调用方法是设置它:
// Set to no-cache if the version requested does not match
bool noCache = false;
var request = context.HttpContext.Request;
if (request != null) {
string queryVersion = request.QueryString.Get(VersionQueryString);
if (queryVersion != null && bundleResponse.GetContentHashCode() != queryVersion) {
noCache = true;
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我们已经切换到使用Azure CDN作为我们的捆绑包,并根据程序集版本将版本查询字符串参数更改为类似?v = 1.0.0.0(类似于此问题).捆绑代码将"1.0.0.0"与捆绑包内容的SHA256哈希代码进行比较,并将结果标记为无缓存的捆绑包.
我通过更新查询字符串以匹配内容哈希来解决这个问题.
不幸的是,GetContentHashCode方法的访问级别标记为内部,因此有必要复制实现.我最终创建了一个继承自Bundle的类,以便它可以将版本号作为转换应用于CdnPath:
public class ProxiedCdnBundle : Bundle
{
private readonly string _cdnHost;
public ProxiedCdnBundle(string virtualPath, string cdnHost = "")
: base(virtualPath)
{
_cdnHost = cdnHost;
}
public override BundleResponse ApplyTransforms(BundleContext context, string bundleContent, IEnumerable<BundleFile> bundleFiles)
{
var response = base.ApplyTransforms(context, bundleContent, bundleFiles);
if (context.BundleCollection.UseCdn && !String.IsNullOrWhiteSpace(_cdnHost))
{
string path = System.Web.VirtualPathUtility.ToAbsolute(context.BundleVirtualPath);
base.CdnPath = string.Format("{0}{1}?v={2}", _cdnHost, path, GetBundleHash(response));
}
return response;
}
private static string GetBundleHash(BundleResponse response)
{
using (var hashAlgorithm = CreateHashAlgorithm())
{
return HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content)));
}
}
private static SHA256 CreateHashAlgorithm()
{
if (CryptoConfig.AllowOnlyFipsAlgorithms)
{
return new SHA256CryptoServiceProvider();
}
return new SHA256Managed();
}
}
Run Code Online (Sandbox Code Playgroud)
问题似乎出在 Microsoft.AspNet.Web.Optimization NuGet 包上。将版本从 1.3.0 降级到 1.1.0 后,一切似乎都工作正常。