如何在MVC4中点击浏览器后退按钮时清除浏览器缓存?

如何在MVC4中点击浏览器后退按钮时清除浏览器缓存?

问题描述:

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

I know this is a popular question in *. 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();
}