Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - How to undo Response.Cache.SetNoStore()?

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...