猿问

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

我正在明确提到禁用浏览器缓存页面所需的ASP.NET代码。有很多方法可以影响HTTP标头和元标记,我得到的印象是需要不同的设置才能使不同的浏览器正常运行。获得一个评论的参考位以表明哪些适用于所有浏览器以及哪些适用于特定浏览器(包括版本)是非常好的。


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

    }

}


蝴蝶刀刀
浏览 640回答 3
3回答

小怪兽爱吃肉

我尝试了各种组合,让它们在FireFox中失败了。已经有一段时间了,所以上面的答案可能会正常工作,或者我可能错过了一些东西。对我来说一直有用的是在每个页面的头部或模板(.net中的母版页)中添加以下内容。<script language="javascript" type="text/javascript">&nbsp; &nbsp; window.onbeforeunload = function () {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // This function does nothing.&nbsp; It won't spawn a confirmation dialog&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // But it will ensure that the page is not cached by the browser.&nbsp; &nbsp; }&nbsp;&nbsp;</script>这已经禁止所有浏览器中的所有缓存。
随时随地看视频慕课网APP
我要回答