我正在明确参考禁用浏览器缓存页面所需的 ASP.NET 代码。有很多方法可以影响 HTTP 标头和元标记,我的印象是需要不同的设置才能让不同的浏览器正常运行。获得注释的参考代码位以指示哪些代码适用于所有浏览器以及特定浏览器(包括版本)需要哪些代码,这将是非常棒的。
那里有大量关于这个问题的信息,但我还没有找到一个很好的参考资料来描述每种方法的好处以及特定技术是否已被更高级别的 API 取代。
我对 ASP.NET 3.5 SP1 特别感兴趣,但最好也能获得早期版本的答案。
此博客条目 Two Important Differences between Firefox and IE Caching 描述了一些 HTTP 协议行为差异。
以下示例代码说明了我感兴趣的类型
public abstract class NoCacheBasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DisableClientCaching();
}
private void DisableClientCaching()
{
// Do any of these result in META tags e.g. <META HTTP-EQUIV="Expire" CONTENT="-1">
// HTTP Headers or both?
// Does this only work for IE?
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// Is this required for FireFox? Would be good to do this without magic strings.
// Won't it overwrite the previous setting
Response.Headers.Add("Cache-Control", "no-cache, no-store");
// Why is it necessary to explicitly call SetExpires. Presume it is still better than calling
// Response.Headers.Add( directly
Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-1));
}
}
这是我们在 ASP.NET 中使用的:
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
它在 Firefox 和 IE 中停止缓存,但我们还没有尝试过其他浏览器。这些语句添加了以下响应标头:
Cache-Control: no-cache, no-store
Pragma: no-cache
对于它的价值,我只需要在我的 ASP.NET MVC 3 应用程序中处理它。这是我在 Global.asax 文件中用于处理所有请求的代码块。
protected void Application_BeginRequest()
{
//NOTE: Stopping IE from being a caching whore
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetValidUntilExpires(true);
}
HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false)
的作用是防止在 bith IE 和 FireFox 中进行缓存
我尝试了各种组合并让它们在 FireFox 中失败。已经有一段时间了,所以上面的答案可能很好,或者我可能错过了一些东西。
一直对我有用的是将以下内容添加到每个页面的头部或模板(.net 中的母版页)。
<script language="javascript" type="text/javascript">
window.onbeforeunload = function () {
// This function does nothing. It won't spawn a confirmation dialog
// But it will ensure that the page is not cached by the browser.
}
</script>
这已经为我禁用了所有浏览器中的所有缓存。
我知道有两种方法。首先是告诉浏览器不要缓存页面。将 Response 设置为 no cache 可以解决这个问题,但是您怀疑浏览器通常会忽略此指令。另一种方法是将响应的日期时间设置为未来的某个时间点。我相信所有浏览器在将页面添加到缓存时都会将其更正为当前时间,但在进行比较时它会将页面显示为较新的页面。我相信在某些情况下可能没有进行比较。我不确定细节,它们会随着每个新的浏览器版本而改变。最后一点我有更好的“刷新”自己的页面(另一个响应指令)。刷新似乎不太可能来自缓存。
希望有帮助。
我将测试将 no-store 标签添加到我们的网站,看看这是否会对浏览器缓存产生影响(Chrome 有时会缓存页面)。我还发现这篇文章对关于缓存如何以及为什么工作的文档非常有用,如果 no-store 不可靠,我将查看 ETag 的下一个:
http://www.mnot.net/cache_docs/
http://en.wikipedia.org/wiki/HTTP_ETag
另请参阅 How to prevent google chrome from caching my inputs, esp hidden ones when user click back?,如果没有它,Chrome 可能会重新加载,但会保留 <input>
元素的先前内容 - 换句话说,使用 autocomplete="off"
。
不定期副业成功案例分享
SetCacheability
。SetNoStore
是一种 IE6 解决方法。请参阅Why both no-cache and no-store should be used in HTTP response?。