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

前端 未结 2 560
日久生厌
日久生厌 2020-11-28 07:25

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 Ac

2条回答
  •  鱼传尺愫
    2020-11-28 08:13

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

    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();
    }
    

提交回复
热议问题