MVC Back button issue

淺唱寂寞╮ 提交于 2019-12-01 01:53:37

There is no way to know, on the server side, if the page request was the result of the back button or not.

More than likely, the previous request was a post rather than a get, and the post requires that you repost the data.

Try the following, works great for me:

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();
    }
}

public class HomeController : Controller
{
    [NoCache]
    public ActionResult Index()
    {
        // When we went to Foo and hit the Back button this action will be executed
        // If you remove the [NoCache] attribute this will no longer be the case
        return Content(@"<a href=""/home/foo"">Go to foo</a><div>" + DateTime.Now.ToLongTimeString() +  @"</div>", "text/html");
    }

    public ActionResult Foo()
    {
        return Content(@"<a href=""/home/index"">Go back to index</a>", "text/html");
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!