ASP.NET MVC OutputCache Does Not Save Custom Headers

My application uses ASP.NET MVC 5 with OutputCache (in particular, we use MVCDonutCaching) to cache high traffic sites and expensive routes.

Some of the Actions have a custom ActionFilter that adds a Content-Range header depending on the view model. Without caching, it works like a charm. When the cache is turned on, the first hit is in order (the Content-Range header is present in the response) - but the second one contains only the Content-Type and HTML / JSON response and our custom Content-Range header is missing (which breaks client functions).

Is there a way to enable proper header caching without writing my own implementation of OutputCache?

Many thanks.

+5
source share
1 answer

A cached response is a "304 - unchanged" HTTP response, and such a response should not return object headers (except for some exceptions, such as "Last-Modified").

The title of the Content-Range header you are trying to return is the title of the object:

http://www.freesoft.org/CIE/RFC/2068/178.htm

Here is the complete list of Entity headers:

https://tools.ietf.org/html/rfc2616#section-7.1

The reason 304 does not return object headers is because answer 304 should not return the full representation of the target resource, since nothing has changed.

A status code of 304 (unchanged) indicates that a conditional GET or HEAD was received and would result in 200 (OK) if it were not for the fact that the condition has evaluates to false. In other words, the server is not needed to transmit the representation of the target resource, because the request indicates that the client who made the conditional request already has a valid representation;

This means that the object headers should not be transmitted again. This provides consistency and also has some performance benefits.

If the conditional GET has used strong cache validation (see section 13.3.3), the response MUST NOT include other entity headers. Otherwise (i.e., the Conditional GET used a weak validator), the response MUST NOT include other object headers; this prevents inconsistencies between cached entities and updated headers.

https://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-23#section-4.1

I came to the conclusion that ASP.NET and IIS correctly interpret this specification, and what you are trying to do is NOT supported. Proof of this is that Apache and other popular web servers do the same as described above.

If you still need this header in your 304, you will have to identify and replace (if possible) the components responsible for filtering the 304 responses.

+1
source

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


All Articles