How to Clear Browser Cache on Browser Back Button Click in MVC4

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

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

How to re request page on back button click in asp mvc

You have to set the pages to expire immediately. For example:

    // disabling caching for all parent pages
protected override void OnInit( EventArgs e ) {
base.OnInit(e);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AppendHeader("Cache-Control", "no-cache, no-store");
Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-1));
}

I have that in my master pages. This tells the browser NOT to cache the page. Because it's no longer cached, if the person hits the back button it will cause the browser to send the page request to your server, at which point you can redirect them to a login page.

MVC4 - Clearing form after submission to hide values from browser's back function

In short - you can not. If you want more detail on why there is no proper way to do this and some ideas on how to make it partially work - you might want to read this article

MVC 4 - back button issue after logout

You could use the hash change event on the browser window, to trigger an ajax request on postback, this would obviously fail as your logged out. From there you could trigger the browser to do anything you like.

After logout if browser back button press then it go back last screen

I had this problem a while ago, disabling the cache for the entire application solved my problem, just add these line to the Global.asax.cs file

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

Hope this helps.

Refresh Page on Back Click - MVC 4

Disabling cache on the ActionResult forces the page to refresh each time rather than rendering the cached version.

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public ActionResult Index()
{
//do something always

return View();
}

Now when you click the browsers back button, this is hit every time.



Related Topics



Leave a reply



Submit