开发者

MVC Back button issue

开发者 https://www.devze.com 2023-03-19 06:46 出处:网络
I have an action method that I need to execute when the back button is clicked. I\'ve done this before by disabling the cache in my action method (Response.Cache.SetCacheability(HttpCacheability.NoCac

I have an action method that I need to execute when the back button is clicked. I've done this before by disabling the cache in my action method (Response.Cache.SetCacheability(HttpCacheability.NoCache). This isn't working for a different action method. For some reason when i disable the cache and 开发者_JS百科hit the back button to trigger my action method the page expires. Any ideas on what the issue may be?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号