In my AssetsController
I have an endpoint:
[HttpGet]
public async Task<IActionResult> Image([FromQuery, Required] long id)
{
// code removed for readability
if(fileNotFound)
{
// Send no-cache header
// returns "placeholder" image
}
// If file found -> set cache
}
I want to set up caching, so that the browser doesn't have to load the resource every time I visit.
Each image gets an assigned id which stays the same, meaning that data returned by a specific id doesn't change. This endpoint is used to return ui elements, like icons.
What would be the easiest/best way to enable caching only for this endpoint?
After playing around a bit I have found a way to do this.
I do it like this and it seems to be working just fine:
[HttpGet]
public async Task<IActionResult> Image([FromQuery, Required] long id)
{
// code removed for readability
if(fileNotFound)
{
// Send no-cache header
Response.Headers.CacheControl = "no-cache";
// returns "placeholder" image
}
// If file found -> set cache
Response.Headers.CacheControl = "max-age=2630000"; // 1 month
}