如何在MVC4中点击浏览器后退按钮时清除浏览器缓存? [英] How to clear browser cache on browser back button click in MVC4?

查看:40
本文介绍了如何在MVC4中点击浏览器后退按钮时清除浏览器缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是 stackoverflow 中的一个流行问题.我已经解决了每个相同的问题,但我无法为我找到正确的答案.这是我的注销控制器操作结果

I know this is a popular question in stackoverflow. I have gone through every same question and I am unable to find the right answer for me. This is my log out controller Action Result

    [Authorize]       
    public ActionResult LogOut(User filterContext)
    {
        Session.Clear();
        Session.Abandon();
        Session.RemoveAll();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
        FormsAuthentication.SignOut();
        return RedirectToAction("Home", true);

    }

它对我不起作用.我也尝试添加 -

It didn't work for me. I also tried adding-

<meta http-equiv="Cache-Control" content="no-cache"/><meta http-equiv="Pragma" content="no-cache"/><meta http-equiv="Expires" content="0"/>

这些都没有解决我的问题.

none of these resolved my issue.

推荐答案

你的方法的问题在于你将它设置在 MVC 应用它已经为时已晚的地方.以下三行代码应该放在显示您不想显示的视图(因此是页面)的方法中.

The problem with your approach is that you are setting it where it is already too late for MVC to apply it. The following three lines of your code should be put in the method that shows the view (consequently the page) that you do not want to show.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

如果你想在所有页面上应用浏览器后无缓存"行为,那么你应该把它放在 global.asax 中.

If you want to apply the "no cache on browser back" behavior on all pages then you should put it in global.asax.

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}

这篇关于如何在MVC4中点击浏览器后退按钮时清除浏览器缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆