我正在明确提到禁用浏览器缓存页面所需的ASP.NET代码。有很多方法可以影响HTTP标头和元标记,我得到的印象是需要不同的设置才能使不同的浏览器正常运行。获得一个评论的参考位以表明哪些适用于所有浏览器以及哪些适用于特定浏览器(包括版本)是非常好的。
关于这个问题有大量的信息,但我还没有找到一个很好的参考资料来描述每种方法的好处,以及某种技术是否已被更高级别的API取代。
我对ASP.NET 3.5 SP1特别感兴趣,但同样可以获得早期版本的答案。
此博客文章Firefox和IE缓存之间的两个重要差异描述了一些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));
}
}
小怪兽爱吃肉