ASP.NET Web API CacheControl

I am trying to implement CacheControl headers from an ASP.NET Web Api action (I understand that I can do this with other libraries, as well as in filters / handlers, but first I want to do some tests).

I follow a trivial example from a book that looks like this:

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; 

This code is almost identical to a number of other answers provided in stackoverflow.

However, the web api does not set the cache control header at all! Any ideas ?????

When I look at the answer in a script, it looks like this, since you can configure cache cache without a 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 

I tried changing maxage, mustrevalidate and public values ​​without any benefit ...

+6
source share
2 answers

Check the pipeline of your request to see if you have something that processes your response through the System.Web.HttpResponse object (for example: HttpContext.Current.Response ). I came across cases where adding http cookies through System.Web.HttpResponse destroy the cache control headers defined in the Web API System.Net.Http.HttpResponseMessage .

Try to avoid System.Web.HttpResponse . If this is not possible, one workaround, as discussed here , would be to manually set the cookie header using AddHeader() and generally avoid the Cookies collection.

+2
source

The answer you are looking for is discussed in this answer stream .

The real problem is that the ASP.Net Web API is an architectural disaster with a terrible design and even more terrible documentation. But as far as I can tell, this is the only library (other than WFC, which is another terribly bad design / architecture) that Microsoft ignores, it’s stupid to "match everything with the file extension!". requirements and provides real REST API endpoints.

You will spend a lot of time figuring out what just broke through, as if you cannot communicate with the response headers, but this is better than a few hundred services.

And if someone out there knows a reasonably designed library that can deliver true REST endpoints and behavior without going around my arm and two legs, I would certainly like to hear about that.

-4
source

Source: https://habr.com/ru/post/945834/


All Articles