23

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

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

1
  • 1
    Back to where? Anywhere? Or just a specific action?
    – mattytommo
    May 2, 2013 at 11:41

2 Answers 2

51

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();
}
3
  • 4
    Why has this not been accepted and why has it not been upvoted more? After hours of Googling this is the only solution that works across all browsers. Jul 4, 2013 at 11:25
  • 3
    sir, you are my hero for today.
    – Saturnix
    Nov 18, 2013 at 14:14
  • 1
    this will impact the concept of bundles inside MVC application, the js & css files will not get cached and each reload or back the js & css files will load again from server instead of browser cache Sep 8, 2019 at 11:40
14

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

1
  • This really helped me. This solution is best for such case if you want to cache static files/images etc but don't want to cache pages (views).
    – mmushtaq
    Dec 28, 2016 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.