Prevent back button

ErocM picture ErocM · Oct 19, 2012 · Viewed 19.9k times · Source

I am using Razor on ASP.NET MVC with C#.

I am calling an external web page to process a credit card and it returns to me. I then display a receipt.

I'd like to prevent them from going back to the previous screen.

I do not have an underlying cs page, like asp since these are .cshtml files, to grab the event.

This receipt page is a View so I cannot put JavaScript in the header since it would affect every page using it.

Anyone know how I prevent the back button in this circumstance?

Answer

Darin Dimitrov picture Darin Dimitrov · Oct 19, 2012

One possibility is to exclude the page you don't want to get back to from caching on the client. This could be done by setting the proper response headers. Here's an example with a [NoCache] custom action filter which you could use to decorate the corresponding controller action.