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?

0

1 Answer 1

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();
    }
} 
Sign up to request clarification or add additional context in comments.

5 Comments

Am going to try that now, thanks...
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 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
Your answer does do as I asked essentially, will mark it accordingly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.