I am using ASP.NET MVC (latest version).
Imagine you have 2 pages:
Page-1: "Enter data" → Page-2: "Thank you"
After sending page-1, you are redirected to page-2.
My goal: . I want to make sure that you cannot return to page-1 when you click the back-to-back button as soon as you go to page-2. Instead, I want you to stay on page-2 (or click on page-2 every time you click the back button).
I tried all different things. Below is just a simplified pseudo code ...
[NoBrowserCache]
public ActionResult Page1(int userId)
{
var user = GetUserFromDb(userId);
if (user.HasAlreadySubmittedPage1InThePast)
{
return RedirectToAction("Page2", routeValues: new { userId = userId });
}
var model = new Page1Model();
return View("Page1", model);
}
[NoBrowserCache]
[HttpPost]
public ActionResult Page1(Page1Model model)
{
var user = GetUserFromDb(model.UserId);
if (user.HasAlreadySubmittedPage1InThePast)
{
return RedirectToAction("Page2", routeValues: new { userId = model.UserId });
}
return RedirectToAction("Page2", routeValues: new { userId = model.UserId });
}
public class NoBrowserCache : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-cache");
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now);
filterContext.HttpContext.Response.Expires = 0;
}
}
, , . , "", -1 , . -2 .
?
, !
Btw: /. , Session.Abandon() - . - , javascript, .
EDIT 2017-5-12
@grek40, , -chaching-. [NoBrowserCache] -ActionFilterAttribute # . <head> _Layout.cshtml:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
, ( ). . - . Google Chrome v62, Firefox Quantum v57 Microsoft Edge v41 ( Win10). #
EDIT 2017-6-12
@grek40 : try : 0, : -1. . .
