【发布时间】:2011-08-29 05:50:30
【问题描述】:
我有一个在单击后退按钮时需要执行的操作方法。我之前通过在我的操作方法(Response.Cache.SetCacheability(HttpCacheability.NoCache)中禁用缓存来完成此操作。这不适用于不同的操作方法。由于某种原因,当我禁用缓存并点击后退按钮时触发我的操作方法页面过期。关于问题可能是什么的任何想法?
【问题讨论】:
标签: asp.net-mvc-2
我有一个在单击后退按钮时需要执行的操作方法。我之前通过在我的操作方法(Response.Cache.SetCacheability(HttpCacheability.NoCache)中禁用缓存来完成此操作。这不适用于不同的操作方法。由于某种原因,当我禁用缓存并点击后退按钮时触发我的操作方法页面过期。关于问题可能是什么的任何想法?
【问题讨论】:
标签: asp.net-mvc-2
尝试以下方法,对我很有用:
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");
}
}
【讨论】:
在服务器端无法知道页面请求是否是返回按钮的结果。
以前的请求很可能是帖子而不是获取,并且帖子要求您重新发布数据。
【讨论】: