我正在尝试从ASP.NET Web Api操作中实现CacheControl标头(我意识到我可以使用其他库,在过滤器/处理程序中执行此操作,但希望先进行一些测试)。
我正在阅读一本看起来像这样的书的简单例子:
var response = Request.CreateResponse<IEnumerable<string>>(HttpStatusCode.OK,emails);
response.Headers.CacheControl = new CacheControlHeaderValue();
response.Headers.CacheControl.MaxAge = TimeSpan.FromHours(1);
response.Headers.CacheControl.MustRevalidate = false;
response.Headers.CacheControl.Public = true;
return response;
此代码几乎与stackoverflow上提供的其他答案相同。
但是,web api根本没有设置缓存控制头!任何想法?????
当我查看fiddler中的响应时,它看起来如下所示,因为你可以将缓存控制设置为no-cache。
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-MiniProfiler-Ids: ["b7bfde10-e8d3-455d-b40d-2f33eb285023"]
X-Powered-By: ASP.NET
Date: Sat, 25 May 2013 11:54:35 GMT
Content-Length: 24
我尝试过更改maxage,mustrevalidate和public值都无济于事......
答案 0 :(得分:2)
检查您的请求管道,看看您是否有通过System.Web.HttpResponse
对象操作响应的内容(例如:HttpContext.Current.Response
)。我遇到过通过System.Web.HttpResponse
添加http Cookie会消除Web API System.Net.Http.HttpResponseMessage
中定义的任何缓存控制标头的情况。
尝试避免System.Web.HttpResponse
。如果无法做到这一点,one workaround as discussed here将通过AddHeader()
手动设置Cookie标头,并完全避免Cookies
收集。