How to handle form submission ASP.NET MVC Back button?

前端 未结 3 1301
梦如初夏
梦如初夏 2020-12-11 06:44

i have a form which allows the user to key in the data and then submit. if everything works well on this action result, then i will redirect the user back to a thank you pag

3条回答
  •  一个人的身影
    2020-12-11 07:01

    This solution works perfectly for both the whole controller or a specific action, simply add [NoCache]

     /// 
     /// Prevent a controller or specific action from being cached in the web browser.
     /// For example - sign in, go to a secure page, sign out, click the back button.
     /// 
     /// 
    public class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var response = filterContext.HttpContext.Response;
            response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            response.Cache.SetValidUntilExpires(false);
            response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.Cache.SetNoStore();
        }
    }
    

    And in your code:

    [NoCache]
    [Authorize]
    public class AccountController : Controller
    { ... }
    

    Originally posted here: MVC Back button issue

提交回复
热议问题