如何撤消Response.Cache.SetNoStore()?

我有一个CMS应用程序代码,它在所有请求上调用Response.Cache.SetNoStore() ,如果我是正确的,这将阻止代理/ cdn缓存这些页面/内容。 因此,我有条件地调用以下代码:

 Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetMaxAge(new TimeSpan(0, 30, 0)); Response.Cache.SetValidUntilExpires(true); 

但这并没有从响应头中取出no-store参数,这是返回的http头:

 Cache-Control:public, no-store, must-revalidate, max-age=1800 

因此,我的问题是,如何才能真实地取出nostore param? 如果这是不可能的,我如何/在哪里解析/修改http-header,因为我试图在PagePreRender事件上解析并且nostore param尚未应用…这导致想知道哪个生命周期是这个附加到标题?

有一种方法可以在调用后撤消SetNoStore 。 您需要使用一些创意路由以不同的方式处理请求或reflection以调用私有的内置重置。

您可以访问HttpCachePolicyWrapper以访问底层HttpCachePolicy ,然后分配内部NoStore字段或发出Reset以恢复为默认缓存策略。

 response.Cache.SetNoStore(); // assign no-store BindingFlags hiddenItems = BindingFlags.NonPublic | BindingFlags.Instance; var httpCachePolicyWrapper = response.Cache.GetType(); // HttpCachePolicyWrapper type var httpCache = httpCachePolicyWrapper.InvokeMember("_httpCachePolicy", BindingFlags.GetField | hiddenItems, null, response.Cache, null); var httpCachePolicy = httpCache.GetType(); // HttpCachePolicy type // Reset Cache Policy to Default httpCachePolicy.InvokeMember("Reset", BindingFlags.InvokeMethod | hiddenItems, null, httpCache, null); var resetAllCachePolicy = httpCachePolicy.InvokeMember("_noStore", BindingFlags.GetField | hiddenItems, null, httpCache, null); response.Cache.SetNoStore(); // assign no-store // Undo SetNoStore Cache Policy httpCachePolicy.InvokeMember("_noStore", BindingFlags.SetField | hiddenItems, null, httpCache, new object[] { false }); var resetNoStoreOnly = httpCachePolicy.InvokeMember("_noStore", BindingFlags.GetField | hiddenItems, null, httpCache, null);