gpt4 book ai didi

asp.net - 从 ASP.NET 禁用所有浏览器的浏览器缓存

转载 作者:可可西里 更新时间:2023-11-01 15:03:31 29 4
gpt4 key购买 nike

我正在寻找禁用浏览器缓存页面所需的 ASP.NET 代码的明确引用。影响 HTTP header 和元标记的方法有很多,我的印象是需要不同的设置才能使不同的浏览器正常运行。如果能获得一段引用代码注释以指明哪些代码适用于所有浏览器,哪些是特定浏览器(包括版本)所必需的,那将是非常棒的。

那里有大量关于这个问题的信息,但我还没有找到一个很好的引用来描述每种方法的好处以及特定技术是否已被更高级别的 API 取代。

我对 ASP.NET 3.5 SP1 特别感兴趣,但最好也能得到早期版本的答案。

此博客条目 Two Important Differences between Firefox and IE Caching描述了一些 HTTP 协议(protocol)行为差异。

下面的示例代码说明了我感兴趣的事情

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 中停止缓存,但我们还没有尝试过其他浏览器。这些语句添加了以下响应 header :

Cache-Control: no-cache, no-store
Pragma: no-cache

关于asp.net - 从 ASP.NET 禁用所有浏览器的浏览器缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/914027/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com