Setting Expires/Caching headers in a Web API Formatter
asp.net, asp.net-web-api
Solution
Seems any setting of `HttpResponseMessage.Headers.CacheControl` causes the Expires header to be emitted as set, but without it set Expires is emitted with a value of -1. Try setting `response.Headers.CacheControl = new CacheControlHeaderValue()` but without setting max-age. You should be able to do this anywhere that exposes `HttpResponseMessage`; e.g. in the `ApiController` or a `DelegatingHandler`.
As per RFC2616, if CacheControl's max-age is present, it overrides Expires, but if you just set it as above it should work.
Whether or not this is a good idea is debatable, as Expires is HTTP 1.0, while CacheControl is HTTP 1.1.
Problem
Is is possible to set the expiration/caching headers inside of a MediaTypeFormatter in Web API? I have tried overriding the SetDefaultContentHeaders and setting the expires header like so: ``` public override void SetDefaultContentHeaders(Type type, System.Net.Http.Headers.HttpContentHeaders headers, System.Net.Http.Headers.MediaTypeHeaderValue mediaType) { headers.Expires = DateTime.Now.AddHours(24); } ``` But the expires header always comes back with -1 when viewing it in a web debugger like the Chrome tools. It also doesn't appear that setting the CacheControl header is not possible here as that is a response header and not a content header (whatever that means).