从 ASP.NET 禁用所有浏览器的浏览器缓存 [英] Disabling browser caching for all browsers from ASP.NET
问题描述
我正在明确参考禁用浏览器缓存页面所需的 ASP.NET 代码.有很多方法可以影响 HTTP 标头和元标记,我的印象是需要不同的设置才能让不同的浏览器正确运行.如果能对代码的参考位进行注释以指明哪些适用于所有浏览器,哪些适用于特定浏览器(包括版本),那就太好了.
I'm after a definitive reference to what ASP.NET code is required to disabled browsers from caching the page. There are many ways to affect the HTTP headers and meta tags and I get the impression different settings are required to get different browsers to behave correctly. It would be really great to get a reference bit of code commented to indicate which works for all browsers and which is required for particular browser, including versions.
那里有大量关于这个问题的信息,但我还没有找到一个很好的参考资料来描述每种方法的好处以及特定技术是否已被更高级别的 API 取代.
There is a huge amount of information about this issue there but I have yet to find a good reference that describes the benefits of each method and whether a particular technique has been superseded by a higher level API.
我对 ASP.NET 3.5 SP1 特别感兴趣,但如果能得到早期版本的答案也会很好.
I'm particularly interested in ASP.NET 3.5 SP1 but it would be good to get answers for earlier version as well.
此博客条目 Firefox 和 IE 缓存之间的两个重要差异描述了一些 HTTP 协议行为差异.
This blog entry Two Important Differences between Firefox and IE Caching describes some HTTP protocol behaviour differences.
下面的示例代码说明了我感兴趣的东西
The following sample code illustrates the kind of thing I am interested in
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 中使用的:
This is what we use in ASP.NET:
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
它在 Firefox 和 IE 中停止缓存,但我们还没有尝试过其他浏览器.这些语句添加了以下响应标头:
It stops caching in Firefox and IE, but we haven't tried other browsers. The following response headers are added by these statements:
Cache-Control: no-cache, no-store
Pragma: no-cache
这篇关于从 ASP.NET 禁用所有浏览器的浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!