Using ASP.NET Core 3.1 I created a Middleware as follows:
public class SitemapMiddleware {
private readonly RequestDelegate _next;
public SitemapMiddleware(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context, ISitemapService sitemapService) {
if (context.Request.Path.StartsWithSegments("sitemap.xml")) {
Sitemap sitemap = await sitemapService.GetAsync();
context.Response.ContentType = "application/xml";
context.Response.Headers.Add("Cache-Control", "public,max-age=20");
await context.Response.WriteAsync(sitemap.Build(), Encoding.UTF8);
} else {
await _next(context);
}
}
}
When I access "sitemap.xml" in the browser the response I get has the correct content.
But when I access it again within the next 20 seconds the code is executed again.
I am adding the "Cache-Control" header and I checked and it is in the response.
Why is the code executed again?