如何为Kestrel响应添加no-cache?

Ali*_*ned 4 asp.net kestrel-http-server asp.net-core

我使用Asp.Net Core RC2和Kestrel作为我的Web服务器.我需要确保使用no-cache标头响应请求(在这种情况下所有这些请求),以便浏览器获得最新版本(而不是304).

在Startup中有没有办法配置Kestrel或将此步骤注入管道?

编辑:在我的情况下,no-store可能是更好的选择:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching "no-store不允许缓存响应并且必须在每次请求时全部取出."

N. *_*len 9

您可以使用中间件来处理标头.例如,您可以通过将以下内容添加到Startup的Configure方法的顶部来强制执行no-cache缓存控制:

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

  • 尼特:您可以使用内置的[`HeaderNames`类](https://github.com/aspnet/HttpAbstractions/blob/release/src/Microsoft.Net.Http.Headers/HeaderNames.cs),以避免第一魔术字符串:`httpContext.Response.Headers [HeaderNames.CacheControl] ="no-cache"`. (3认同)
  • 确保它高于其他 app.Uses(例如 app.UseDefaultFiles 或 app.UseStaticFiles() (2认同)