I would like to return a different cache-control header value for different endpoints using ASP.NET minimal APIs. How do we do this without controllers?
This can be accomplished using controllers as follows:
using Microsoft.AspNetCore.Mvc;
namespace CacheTest;
[ApiController]
public class TestController : ControllerBase
{
[HttpGet, Route("cache"), ResponseCache(Duration = 3600)]
public ActionResult GetCache() => Ok("cache");
[HttpGet, Route("nocache"), ResponseCache(Location = ResponseCacheLocation.None, Duration = 0)]
public ActionResult GetNoCache() => Ok("no cache");
}
The first endpoint returns the header Cache-Control: public,max-age=3600
.
The second endpoint returns the header Cache-Control: no-cache,max-age=0
.