【问题标题】:How to set Response.Cache.SetCacheability in ASP.net WebForms?如何在 ASP.net WebForms 中设置 Response.Cache.SetCacheability?
【发布时间】:2014-09-02 05:01:47
【问题描述】:

如何在 WebForms 中调用 Response.Cache.SetCacheability

如果你看看 MSDN 的How to: Set a Page's Cacheability Programmatically

以编程方式设置页面的可缓存性

  • 在页面代码中,调用Response 对象的Cache 属性上的SetCacheability 方法。

    以下代码将 Cache-Control HTTP 标头设置为Public

    Response.Cache.SetCacheability(HttpCacheability.Public);
    

很好。优秀。好的。除了我该怎么做?

尝试将其添加到 Page_Init 事件处理程序:

protected void Page_Init(object sender, EventArgs e)
{
   Response.Cache.SetCacheability(HttpCacheability.Public); //Public, while we test this
}

但来自服务器的响应并不公开(实际上是私有):

HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Fri, 11 Jul 2014 14:11:06 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 9382
Connection: Close

所以我想确认我的代码正在运行,所以我添加了一些虚拟标题:

protected void Page_Init(object sender, EventArgs e)
{
    //Response.Headers.Add("X-Hello-Before", "WhyArentYouWorking");
    Response.AddHeader("X-Hello-Before", "WhyArentYouWorking");

    Response.Cache.SetCacheability(HttpCacheability.Public); //Client is allowed to cache

    //Response.Headers.Add("X-Hello-After", "MyGodYouSuck");
    Response.AddHeader("X-Hello-After", "MyGodYouSuck");
}

并且项目出现在响应头中:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Fri, 11 Jul 2014 14:16:47 GMT
X-AspNet-Version: 4.0.30319
X-Hello-Before: WhyArentYouWorking
X-Hello-After: MyGodYouSuck
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 9382
Connection: Close

问题是:

我如何让 ASP.net 做我告诉它做的事情?

我不知道错误在哪里。它可能在 ASP.net 中。它可以在 WebForms 中。它可能在 .NET Framework 4.0 中。它可能在卡西尼号。

【问题讨论】:

    标签: asp.net caching webforms browser-cache


    【解决方案1】:

    您之间是否有代理,也许这不允许您更改它?否则,您也可以尝试将其设置为 IIS:

    1. 打开 IIS 管理器,转到树中的应用程序。
    2. 选择您的应用程序并在右侧部分中双击“HTTP 响应标头”选项。
    3. 点击操作面板上的“添加...”

    在弹出的窗口中填写: 名称:缓存控制 价值:公开

    看看是否可行。

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2010-11-25
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      相关资源
      最近更新 更多