我有一个在单击后退按钮时需要执行的操作方法。我之前通过在操作方法(Response.Cache.SetCacheability(HttpCacheability.NoCache))中禁用缓存来完成此操作。这对于不同的操作方法不起作用。出于某种原因,当我禁用缓存并点击后退按钮时要触发我的操作方法,页面会过期。对可能出现的问题有什么想法吗?
尝试以下方法,对我来说效果很好:
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");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)