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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16337149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:47:32  来源:igfitidea点击:

How to clear browser cache on browser back button click in MVC4?

c#asp.net-mvcasp.net-mvc-4browser-cache

提问by Sandy

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

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

    }

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"/>

<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.

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

采纳答案by von v.

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.

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

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

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

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

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

回答by MarkG

Just set the output cache on the action. I have used this approach in many projects:

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

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

The above attribute will basically instruct the browser to get a fresh copy of the page from your controller action if the user navigates back / forward to your view.

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

You can also define your caching in the web.config and use in conjunction with this attribute to avoid some repetition. See here

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