禁用来自 ASP.NET 的所有浏览器的浏览器缓存

我在寻找一个明确的参考资料,说明禁用浏览器缓存页面所需的 ASP.NET 代码。有许多方法可以影响 HTTP 头和 meta 标记,我得到的印象是,不同的浏览器需要不同的设置才能正确运行。如果能够获得一些注释代码,以指出哪些代码适用于所有浏览器,哪些代码是特定浏览器(包括版本)所需的,那将是非常棒的。

这里有大量关于这个问题的信息,但是我还没有找到一个很好的参考资料来描述每种方法的好处,以及某种特定的技术是否已经被更高级别的 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));
}
}
116950 次浏览

There are two approaches that I know of. The first is to tell the browser not to cache the page. Setting the Response to no cache takes care of that, however as you suspect the browser will often ignore this directive. The other approach is to set the date time of your response to a point in the future. I believe all browsers will correct this to the current time when they add the page to the cache, but it will show the page as newer when the comparison is made. I believe there may be some cases where a comparison is not made. I am not sure of the details and they change with each new browser release. Final note I have had better luck with pages that "refresh" themselves (another response directive). The refresh seems less likely to come from the cache.

Hope that helps.

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();

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

For what it's worth, I just had to handle this in my ASP.NET MVC 3 application. Here is the code block I used in the Global.asax file to handle this for all requests.

    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);
}

I've tried various combinations and had them fail in FireFox. It has been a while so the answer above may work fine or I may have missed something.

What has always worked for me is to add the following to the head of each page, or the template (Master Page in .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>

This has disabled all caching in all browsers for me without fail.

I'm going to test adding the no-store tag to our site to see if this makes a difference to browser caching (Chrome has sometimes been caching the pages). I also found this article very useful on documentation on how and why caching works and will look at ETag's next if the no-store is not reliable:

http://www.mnot.net/cache_docs/

http://en.wikipedia.org/wiki/HTTP_ETag

See also How to prevent google chrome from caching my inputs, esp hidden ones when user click back? without which Chrome might reload but preserve the previous content of <input> elements -- in other words, use autocomplete="off".