AspNetCore.CacheOutput icon indicating copy to clipboard operation
AspNetCore.CacheOutput copied to clipboard

Cache attribute changes response status code to 200 regardless of original response code

Open avbelkin opened this issue 3 years ago • 4 comments

This line forces cache output to always set response code 200 when responding with cached answer https://github.com/Iamcerba/AspNetCore.CacheOutput/blob/269322c126a8971bf73836863c7a7dffbc6caa41/AspNetCore.CacheOutput/CacheOutputAttribute.cs#L187

Consider following scenario:

  1. API controller takes parameters and returns NoContent (204) in some cases. It has CacheOutput attribute set on controller method
  2. frontend fetch js api checks if it is 200 or 204. If response code is 200 then fetch does await response.json()
  3. next call to controller with same parameters returns empty body with response code 200. Json parse fails

Could you please add feature to respect response statuses? It would be more performance effective for API controllers then responding with empty dummy jsons. Thanks a lot for amazing work!

avbelkin avatar Feb 04 '23 20:02 avbelkin

Yes, taking into account that HTTP 204 is cacheable current behavior looks like not what at least me expected to see.

Will get back to you later, need to think how to handle this. Don't want server side caching to rely on cached content value to determine status code, original status code ideally should be stored as part of the cache. I'm considering merging all "secondary" cache keys (ContentTypeKey, EtagKey, LastModifiedKey + new status code key) and corresponding values to a single cache entry.

Iamcerba avatar Feb 05 '23 13:02 Iamcerba

https://github.com/Iamcerba/AspNetCore.CacheOutput/compare/master...fix/issue-30

Iamcerba avatar Feb 05 '23 15:02 Iamcerba

Also released pre release packages in NuGet. Check "3.0.0-pre1" version.

Iamcerba avatar Feb 05 '23 17:02 Iamcerba

Thanks a lot, that's really amazing! New version works as expected. All test runs are also fine, at least for InMemory model

avbelkin avatar Feb 05 '23 21:02 avbelkin