【发布时间】:2020-08-10 23:07:54
【问题描述】:
我使用 ASP.NET Core 3.1 创建了如下中间件:
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);
}
}
}
当我在浏览器中访问“sitemap.xml”时,我得到的响应内容正确。
但是当我在接下来的 20 秒内再次访问它时,代码会再次执行。
我正在添加“Cache-Control”标头,我检查了它,它在响应中。
为什么代码又被执行了?
【问题讨论】:
-
是否需要添加Caching Middleware?
-
我在 ConfigureServices 中有
services.AddResponseCaching();在 Configure 方法中有applicationBuilder.UseResponseCaching();。我不知道是否缺少某些东西...... -
在开发者选项下的浏览器上(在 chrome F12/Network 上)通常有一个“禁用缓存”选项。值得一试。
-
我知道那个选项并且我启用了缓存...
标签: asp.net-core asp.net-core-3.1