如何撤消Response.Cache.SetNoStore()? [英] How to undo Response.Cache.SetNoStore()?

查看:326
本文介绍了如何撤消Response.Cache.SetNoStore()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMS应用code这就要求 Response.Cache.SetNoStore()上的所有请求,如果我是正确的,这将是prevents代理/ CDN缓存的页面/内容。所以,我是有条件调用下面code:

I've a CMS application code which calls Response.Cache.SetNoStore() on all request and if i'm correct, this will be prevents proxies/cdn to cache those pages/content. Therefore, i'm conditionally calling the below code:

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

但是,这并不取出从响应头无店面参数,这是返回的HTTP标头:

But this doesn't take out the no-store param from the response header, this is the returned http header:

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

所以我的问题是,我怎么能拿出nostore参数务实?如果这是不可能的,如何/我在哪里可以解析/修改HTTP头,因为我试图解析的页面preRender事件和nostore参数还没有被应用......这导致在该怀疑生命周期此附加到报头

Therefore my question is, how can i take out the nostore param pragmatically? If this isn't possible, how/where can i parse/modify the http-header, because i tried to parsed on PagePreRender event and the nostore param hasn't been applied...which leads to wonder at which life cycle is this appended to the header?

推荐答案

有一种方法可以撤消 SetNoStore 一旦你调用它。你需要使用一些有创意的路由来处理以不同的方式或反射调用内置复位是私人的请求。

There is a way to undo SetNoStore once you call it. You need to use some creative routing to process the request in a different way or reflection to invoke the built-in reset that is private.

您可以访问<一个href=\"https://github.com/Microsoft/referencesource/blob/master/System.Web/Abstractions/HttpCachePolicyWrapper.cs\"相对=nofollow> HttpCachePolicyWrapper 访问底层的 为HttpCachePolicy ,然后分配给内部 NoStore 现场或发行重置来恢复到默认的缓存策略。

You can get access to HttpCachePolicyWrapper to access the underlying HttpCachePolicy, then assign the internal NoStore field or issue Reset to revert to the default cache policy.

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

这篇关于如何撤消Response.Cache.SetNoStore()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆