我正在寻找一个明确的参考,说明禁用浏览器缓存页面所需的ASP.NET代码。有许多方法可以影响HTTP头和元标签,我得到的印象是,需要不同的设置才能让不同的浏览器正确运行。如果能得到一段代码的参考注释,以指出哪些适用于所有浏览器,哪些是特定浏览器所需的,包括版本,那将是非常棒的。
关于这个问题有大量的信息,但我还没有找到一个很好的参考资料来描述每种方法的好处,以及特定的技术是否已经被更高级别的API所取代。
我对ASP.NET 3.5 SP1特别感兴趣,但如果能得到早期版本的答案就更好了。
这篇博客文章Two Important Differences between Firefox and IE Caching描述了一些超文本传输协议行为的差异。
下面的示例代码说明了我感兴趣的内容
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));
}
}
发布于 2009-05-27 22:39:31
这是我们在ASP.NET中使用的:
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
它会在Firefox和IE中停止缓存,但我们还没有尝试过其他浏览器。这些语句添加了以下响应头部:
Cache-Control: no-cache, no-store
Pragma: no-cache
发布于 2011-04-14 22:06:32
无论如何,我只是不得不在我的ASP.NET MVC3应用程序中处理这个问题。下面是我在Global.asax文件中用来处理所有请求的代码块。
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);
}
发布于 2011-11-22 15:21:57
我尝试过各种组合,但在FireFox中都失败了。这已经有一段时间了,所以上面的答案可能工作得很好,或者我可能遗漏了一些东西。
对我来说,一直有效的方法是将以下内容添加到每个页面的头部,或者模板(.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>
这肯定会禁用所有浏览器中的所有缓存。
https://stackoverflow.com/questions/914027
复制相似问题