如何关闭整个 ASP.NET MVC 3 网站的缓存?

发布于 2025-01-08 01:27:04 字数 45 浏览 0 评论 0原文

就像问题所说,我想知道是否可以关闭整个站点的所有控制器和操作上的缓存。谢谢!

Like the question says, I wanted to know if it's possible to turn off caching on all controllers and actions for my entire site. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

焚却相思 2025-01-15 01:27:05

创建一个全局操作过滤器并覆盖 OnResultExecuting()

public class DisableCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
    }
}

然后将其注册到您的 global.asax 中,如下所示:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new DisableCache());
    }

总而言之,它的作用是创建一个 全局操作过滤器 这样这将隐式地应用于所有控制器和所有操作。

Create a Global Action Filter and override OnResultExecuting():

public class DisableCache : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
    }
}

And then register this in your global.asax, like so:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new DisableCache());
    }

In summation, what this does is create a Global Action Filter so that implicitly this will be applied to all Controllers and all Actions.

给妤﹃绝世温柔 2025-01-15 01:27:05

您应该将此方法添加到您的 Global.asax.cs 文件中。

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            Response.AddHeader("Pragma", "no-cache"); // HTTP 1.0.
            Response.AddHeader("Expires", "0"); // Proxies.
        }

这将禁用每个请求(图像、html、js 等)的缓存。

You should add this method to your Global.asax.cs file

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            Response.AddHeader("Pragma", "no-cache"); // HTTP 1.0.
            Response.AddHeader("Expires", "0"); // Proxies.
        }

This disables cache on every request (images, html, js etc.).

扮仙女 2025-01-15 01:27:05

是的,取决于您采取的方法。
我喜欢将操作应用于基本控制器(因此我在那里回复)。您可以在下面的链接中实现过滤器,并将其实现为全局过滤器(在您的 global.asax.cs 中注册)

禁用整个 ASP.NET 网站的浏览器缓存

Yes, depending on the approach you take.
I like applying the actions to a base controller (hence my reply there). You could implement the filter at the link below and implement it as a global filter as well (registered in your global.asax.cs)

Disable browser cache for entire ASP.NET website

眼泪都笑了 2025-01-15 01:27:05

在 web.config 中,您可以添加额外的标头以随每个响应一起发出

<configuration>
    <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Cache-control" value="no-cache"/>
          </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

In web.config you can add additional headers to go out with every response

<configuration>
    <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Cache-control" value="no-cache"/>
          </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文