【发布时间】: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