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
An Elegant Way to Consume (All Bytes of A) Binaryreader
C# Linq Where Date Between 2 Dates
C# Help Reading Foreign Characters Using Streamreader
How to Add Moving Effects to My Controls in C#
Wpf Combobox: Different Template in Textbox and Drop-Downlist
An Elegant Way to Consume (All Bytes of A) Binaryreader
Why Does a Bad Password Cause "Padding Is Invalid and Cannot Be Removed"
Does Filestreamresult Close Stream
Sqlexception:String or Binary Data Would Be Truncated
Frombluetoothaddressasync Iasyncoperation Does Not Contain a Definition for 'Getawaiter' Error
Synchronize Scroll Position of Two Richtextboxes
Get Table-Data from Table-Name in Linq Datacontext
No Database Provider Has Been Configured for This Dbcontext' on Signinmanager.Passwordsigninasync
How to Display the String HTML Contents into Webbrowser Control
JSON.Net Custom Serialization with JSONconverter - How to Get the "Default" Behavior