且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理表单提交ASP.NET MVC后退按钮?

更新时间:2022-11-01 17:22:48

该解决方案完全适用,那么整个控制器或一个特定的动作,只需添加[NOCACHE]

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

 /// <summary>
 /// 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.
 /// <seealso cref="http://***.com/questions/6656476/mvc-back-button-issue/6656539#6656539"/>
 /// </summary>
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();
    }
}

而在你的code:

And in your code:

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

最初张贴在这里: MVC后退按钮问题