首页 文章

如何在ASP.net WebForms中设置Response.Cache.SetCacheability?

提问于
浏览
2

我如何在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
}

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

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

所以我想确认我的代码是否正常工作,所以我添加了一些虚拟 Headers :

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");
}

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

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做我告诉它做的事情?

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

1 回答

  • 0

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

    • 打开IIS管理器,转到树中的应用程序 .

    • 选择您的应用程序,在右侧部分双击"HTTP Respond Headers"选项 .

    • 单击“动作”面板上的"Add…"

    填写弹出窗口:名称:Cache-Control Value:public

    并看看是否有效 .

相关问题