c# - 在 ASP.NET 中为特定页面设置无缓存?

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

<分区>

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

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

我在 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);
}

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

最佳答案

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

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

关于c# - 在 ASP.NET 中为特定页面设置无缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29918021/

相关文章:

C# 应用程序在控制台中使用另一个应用程序

jquery - 切换开关在 PartialView 中不起作用

c# - ASP、超链接 打开小窗口

asp.net-mvc - 如何将 MVC4 客户端与 IdentityServer4 一起使用?

c# - 如果我只在 HttpGet 上验证是否安全?

c# - 大文件的 Delta 编码 - 有好的实现吗?

c# - 方法放在代码中的整体性能

c# - 需要激活一个窗口

asp.net - 使用 jQuery 在 UpdatePanel 中开始和结束请求

asp.net-mvc - 在我们使用 JWT token 的情况下,Asp.net 核心中的 session 和应用程序变量(因此没有基于 cookie 的 session )