【问题标题】:Set no cache in ASP.NET for specific page? [duplicate]在 ASP.NET 中为特定页面设置无缓存? [复制]
【发布时间】:2015-04-28 11:19:28
【问题描述】:

我想将我的 MVC 5 应用程序设置为不缓存我的登录视图的页面(即,如果我的用户在浏览器中按下“返回”以导航到登录,我希望登录视图实际重新加载页)。

这样我可以在用户尝试以其他人身份登录之前将当前用户注销。

我在 Global.asax 中看到有人使用它的例子:

protected void Application_BeginRequest()
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
    Response.Cache.SetNoStore();
    Response.Cache.SetProxyMaxAge(new TimeSpan(0, 0, 0));
    Response.Cache.SetValidUntilExpires(false);
    Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
}

但这会停止缓存每个请求的每个页面。 我相信有办法通过路由或过滤器来做到这一点?也许是方法注释?谁能解释一下?

【问题讨论】:

    标签: c# asp.net asp.net-mvc caching httpcontext


    【解决方案1】:

    为什么不将这些属性添加到您的操作中:

    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 
    

    例子:

    public class MyController : Controller
    {
        [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 
        // will disable caching for Index only
        public ActionResult Index()
        {
           return View();
        }
    } 
    

    【讨论】:

    • 我现在要试试,谢谢...
    • 发生了什么是它会重新加载页面,它只是不会退出我的用户,除非我再次手动重新加载页面......
    • @barnacle.m 如果您想重新加载页面然后使用 javascript 或 jaquery 并在按下后退按钮时使用它来重新加载页面还有其他返回方式,如 alt + 箭头键和退格键和其他也是。 Link
    • 你的回答确实按照我的要求做,会相应地标记它。
    猜你喜欢
    • 2013-07-03
    • 2020-09-09
    • 2015-02-21
    • 1970-01-01
    • 2010-10-03
    • 2015-11-16
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多