Brian Rosamilia
Brian Rosamilia

Reputation: 1486

ASP.NET MVC and IIS -- 'Cache-control' always 'no-cache' for files

No matter what I do in my ASP.NET web API project, 'Cache-control' is set to 'no-cache' when I'm serving files.

I've used IIS, IIS Express, and Cassini. At the end of my action methods, it's always set to private--which is what I want. But whenever I look at the actual response it's no-cache.

I'm setting the ContentDispositionHeaderValue and the MediaTypeHeaderValue (to application/pdf)

Any ideas? I checked the entire project and nowhere am I overriding this on a global level.

Upvotes: 1

Views: 1066

Answers (1)

Brian Rosamilia
Brian Rosamilia

Reputation: 1486

In web API you can't use HttpContext.Current.Headers to modify the headers (You can't even do this if you return a POCO object). The call will work but it won't actually do anything.

You have to actually set the header on the constructed response object

HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
//IE7/8 Cache value to prevent error
result.Headers.CacheControl = new CacheControlHeaderValue()
{
     Private = true,
};

I'm shocked that I seem to be one of a few people who has tried to do this.

Upvotes: 1

Related Questions