Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice of Cache-Control for Bad Request (400)

Currently I have the following action which will tell the client to cache the response for 1200 seconds:

[ResponseCache(Location = ResponseCacheLocation.Client, Duration = 1200)]
[HttpGet("universities")]
public IActionResult GetAllUniversities(string location)
{
    if (/*location not found*/)
      return BadRequest();

    ...
    return Ok(universities);
}

In the response header, when it returns Ok (200), I received the following value:

Cache-Control: private, max-age=1200

Which is perfect as expected.

When I passed the wrong location to the API and the API returns BadRequest (400), it also returns the same Cache-Control value as above.

My question is, is this the best practice? Or should it return no-cache, no-store instead for 400? If it should, how do I return private, max-age=1200 when it's 200 and return no-cache, no-store in .NET Core for this particular action only?

like image 864
muhihsan Avatar asked Oct 26 '25 07:10

muhihsan


1 Answers

You should use Response Caching Middleware in ASP.NET Core which only caches responses for 200 status code responses and ignores other error responses.

For more information how to implement refer - https://learn.microsoft.com/en-us/aspnet/core/performance/caching/middleware?tabs=aspnetcore2x

like image 199
Ipsit Gaur Avatar answered Oct 28 '25 22:10

Ipsit Gaur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!