2

I would like to set my MVC 5 application to not cache the page for my Login view (i.e. I would like the Login view to actually reload if my user has pressed 'Back' in the browser in order to navigate to the Login page).

This is so that I can log the current user out before the user attempts to log in as somebody else.

I saw an example of somebody using this in Global.asax:

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
    Response.Cache.SetNoStore();
    Response.Cache.SetProxyMaxAge(new TimeSpan(0, 0, 0));
    Response.Cache.SetValidUntilExpires(false);
    Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
}

but this stops caching for basically every page on every request. I believe there is a way to do this through routing or filters? Maybe a method annotation? Can anybody explain this?

barnacle.m
  • 2,070
  • 3
  • 38
  • 82

1 Answers1

1

Why don't you add these attributes to your action:

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 

Example:

public class MyController : Controller
{
    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 
    // will disable caching for Index only
    public ActionResult Index()
    {
       return View();
    }
} 
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • Am going to try that now, thanks... – barnacle.m Apr 28 '15 at 11:24
  • What's happening with that is it does reload the page, it just doesn't sign out my user unless I reload the page again manually... – barnacle.m Apr 28 '15 at 11:27
  • [Don't put hyphens after a colon](http://english.stackexchange.com/questions/31060/is-it-proper-to-use-a-colon-followed-immediately-by-a-hyphen). – CodeCaster Apr 28 '15 at 11:30
  • @barnacle.m if you want to reload the page then use javascript or jaquery and use that to reload page if back button is pressed there are other ways of going back like alt + arrorw key and backspace and other too. [Link](http://stackoverflow.com/questions/25801410/detecting-browser-back-button-in-javascript-jquery-without-using-any-external-pl) – शेखर Apr 28 '15 at 11:36
  • Your answer does do as I asked essentially, will mark it accordingly. – barnacle.m Apr 28 '15 at 11:46