【发布时间】:2010-10-29 04:19:06
【问题描述】:
我正在明确参考禁用浏览器缓存页面所需的 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));
}
}
【问题讨论】:
-
如果我不知道你的任务有多可怕,我会尝试回答。控制客户端的缓存就像试图用 10 英尺长的筷子重新排列家具一样。
-
仅涵盖部分问题的大量答案仍然非常有价值。请投入您的 2 美分。
标签: asp.net http browser caching