Manual client-side response caching with HTTP headers
While ASP.NET Core provides comprehensive response caching middleware, sometimes, manually setting cache headers is the quickest solution. This approach lets you implement caching on specific endpoints without modifying the application’s middleware pipeline while maintaining direct control over caching behavior at the controller level. It also helps us better understand how HTTP caching works before we explore the framework’s abstractions. In this recipe, we’ll implement the simplest form of HTTP caching: telling the client (browser) to cache a response for a fixed duration.
Getting ready
Download the starter project here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter10/ManualResponseCache.
Open the included demo web client: simply open the test-client.html
file in your web browser.
How to do it…
- Open the
BooksController.cs
file and, in theGetBooks...