如何清除浏览器后面的浏览器缓存按钮单击MVC4?

San*_*ndy 20 c# asp.net-mvc browser-cache asp.net-mvc-4

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

    [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);

    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

von*_* v. 50

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

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
Run Code Online (Sandbox Code Playgroud)

如果要在所有页面上应用"浏览器后无缓存"行为,则应将其放在global.asax中.

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

  • 为什么没有被接受,为什么没有被更多的支持呢?经过数小时的谷歌搜索,这是唯一适用于所有浏览器的解决方案. (4认同)
  • 先生,你是我今天的英雄. (3认同)

Mar*_*rkG 13

只需在操作上设置输出缓存即可.我在很多项目中都使用过这种方法:

[HttpGet, OutputCache(NoStore = true, Duration = 1)]
public ActionResult Welcome()
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

如果用户向后/向前导航到您的视图,上述属性将基本上指示浏览器从控制器操作获取页面的新副本.

您还可以在web.config中定义缓存,并与此属性结合使用以避免重复.看到这里