23

我知道这是 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);

    }

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

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

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

4

2 回答 2

51

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

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

如果您想在所有页面上应用“浏览器背面无缓存”行为,那么您应该将它放在 global.asax 中。

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
    Response.Cache.SetNoStore();
}
于 2013-05-02T12:01:07.437 回答
14

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

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

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

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

于 2013-05-03T18:07:07.340 回答