代码之家  ›  专栏  ›  技术社区  ›  Martin Hollingsworth

从ASP禁用所有浏览器的浏览器缓存。网

  •  89
  • Martin Hollingsworth  · 技术社区  · 17 年前

    我想要一个明确的参考ASP。NET代码是禁用浏览器缓存页面所必需的。有很多方法可以影响HTTP标头和元标签,我得到的印象是,需要不同的设置才能让不同的浏览器正常运行。如果能注释一段参考代码,说明哪些适用于所有浏览器,哪些是特定浏览器(包括版本)所必需的,那就太好了。

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

    此博客条目 Two Important Differences between Firefox and IE Caching 描述了一些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));
        }
    }
    
    6 回复  |  直到 17 年前
        1
  •  98
  •   HttpWatchSupport    17 年前

    这就是我们在ASP中使用的。净值:

    // 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
    
        2
  •  41
  •   Adam Carr    15 年前

        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);
        }
    
        3
  •  2
  •   Steve    14 年前

    我尝试过各种组合,但在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>
    

        4
  •  1
  •   Pat O    17 年前

    最后一点,我在“刷新”自己的页面(另一个响应指令)方面运气更好。刷新似乎不太可能来自缓存。

    希望这能有所帮助。

        6
  •  0
  •   Community Mohan Dere    9 年前

    另请参阅 How to prevent google chrome from caching my inputs, esp hidden ones when user click back? <input> 元素——换句话说,使用 autocomplete="off" .