在Web API中设置HTTP缓存控制标头 [英] Setting HTTP cache control headers in Web API
问题描述
在WebAPI中为公共缓存服务器设置缓存控制标头的最佳方法是什么?
What's the best way to set cache control headers for public caching servers in WebAPI?
我对服务器上的OutputCache控制不感兴趣,我希望控制CDN端及其他端的缓存(我有单独的API调用,可以为给定的URL无限期地缓存响应),但我所做的一切到目前为止,我已经阅读了引用WebAPI的预发布版本(因此引用了似乎不再存在的东西,例如System.Web.HttpContext.Current.Reponse.Headers.CacheControl),或者仅仅设置了两个http似乎非常复杂标头.
I'm not interested in OutputCache control on my server, I'm looking to control caching at the CDN side and beyond (I have individual API calls where the response can be indefinitely cached for the given URL) but everything I've read thus far either references pre-release versions of WebAPI (and thus references things that seem to no longer exist, like System.Web.HttpContext.Current.Reponse.Headers.CacheControl) or seems massively complicated for just setting a couple of http headers.
有没有简单的方法可以做到这一点?
Is there a simple way to do this?
推荐答案
可以像这样设置缓存控制标头.
The cache control header can be set like this.
public HttpResponseMessage GetFoo(int id)
{
var foo = _FooRepository.GetFoo(id);
var response = Request.CreateResponse(HttpStatusCode.OK, foo);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
Public = true,
MaxAge = new TimeSpan(1, 0, 0, 0)
};
return response;
}
这篇关于在Web API中设置HTTP缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!