Handling a browser back-button press with MVC

If you need MVC to reload a view via a controller method, even when the browser’s back button has been pressed, then you need to tell the browser to disable caching of that page. This can be done using the OutputCache attribute on the controller method that displays the view.

[HttpGet]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] 
public ActionResult MyView()
{
  ...
}

This method will then be called every time the browser displays that view. If you want to pass information to this page (that wasn’t in the original URL), then the ‘next’ view needs to put that data in a Session variable so that it is available when the user presses the back button.

Note that there are also jQuery plugins that can read your browser history but they all seem to have Internet Explorer compatibility issues.

Follow

Get every new post delivered to your Inbox.

Join 70 other followers