似乎,一旦将可缓存性设置Response.Cache
为NoCache
,就无法再次更改它。这是该问题的简单但完整的说明:
protected void Page_Load(object sender, EventArgs e)
{
FieldInfo fi = typeof(HttpCachePolicy).GetField(
"_cacheability",
BindingFlags.NonPublic | BindingFlags.Instance);
// Default value = 6
HttpCacheability first = (HttpCacheability)fi.GetValue(Response.Cache);
// Can change it to Public
Response.Cache.SetCacheability(HttpCacheability.Public);
HttpCacheability second = (HttpCacheability)fi.GetValue(Response.Cache);
// Can change it to Private
Response.Cache.SetCacheability(HttpCacheability.Private);
HttpCacheability third = (HttpCacheability)fi.GetValue(Response.Cache);
// Can change it to NoCache
Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpCacheability fourth = (HttpCacheability)fi.GetValue(Response.Cache);
// Can't go back to Private! Stuck on NoCache
Response.Cache.SetCacheability(HttpCacheability.Private);
HttpCacheability fifth = (HttpCacheability)fi.GetValue(Response.Cache);
}
我错过了什么吗?有没有办法做到这一点?
编辑:当然,如果我用反射设置它,它会起作用,但我担心当你设置它时会发生其他事情,HttpCacheability.NoCache
如果我去幕后我会想念..并且宁愿在正式 -无论如何支持的方式。
EDIT2: 同样的事情似乎发生在Private
; 你只能限制更多吗?
protected void Page_Load(object sender, EventArgs e)
{
FieldInfo fi = typeof(HttpCachePolicy).GetField(
"_cacheability",
BindingFlags.NonPublic | BindingFlags.Instance);
// Default value = 6
HttpCacheability first = (HttpCacheability)fi.GetValue(Response.Cache);
// Can change it to Private
Response.Cache.SetCacheability(HttpCacheability.Private);
HttpCacheability second = (HttpCacheability)fi.GetValue(Response.Cache);
// Can't change to Public! Stuck on Private
Response.Cache.SetCacheability(HttpCacheability.Public);
HttpCacheability third = (HttpCacheability)fi.GetValue(Response.Cache);
// Can change to NoCache - Can only go more restrictive?
Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpCacheability fourth = (HttpCacheability)fi.GetValue(Response.Cache);
}