问题描述
什么是设置缓存控制标头中的WebAPI公共缓存服务器的最好方法?
What's the best way to set cache control headers for public caching servers in WebAPI?
我不感兴趣,在我的服务器上的OutputCache控制,我期待在CDN侧及以后(我在这里的响应可以无限期地缓存指定网址单独的API调用),但一切我来控制缓存已经迄今为止读取这两种引用$ p $的WebAPI的p-发行版本(因此引用的东西,似乎不复存在,就像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;
}
这篇关于在设置的WebAPI HTTP缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!