如何将 no-cache 添加到 Kestrel 响应中?

How do I add no-cache to Kestrel responses?

我正在使用 Asp.Net Core RC2 和 Kestrel 作为我的网络服务器。我需要确保请求(在本例中是所有请求)都以 no-cache header 响应,以便浏览器获得最新版本(不是 304)。

是否有在 Startup 中配置 Kestrel 或将此步骤注入管道的方法?

编辑:no-store 在我的情况下可能是更好的选择:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching "no-store Response is not allowed to be cached and must be fetched in full on every request."

您可以使用中间件来处理 headers。例如,您可以通过将以下内容添加到 Startup 的 Configure 方法的顶部来强制 no-cache cache-control:

app.Use(async (httpContext, next) =>
{
    httpContext.Response.Headers[HeaderNames.CacheControl] = "no-cache";
    await next();
});