【问题标题】:How do I add no-cache to Kestrel responses?如何向 Kestrel 响应添加无缓存?
【发布时间】:2016-05-03 04:26:19
【问题描述】:

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

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

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

【问题讨论】:

    标签: asp.net asp.net-core kestrel-http-server


    【解决方案1】:

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

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

    【讨论】:

    • nit:可以使用内置的HeaderNames class来避开第一个魔串:httpContext.Response.Headers[HeaderNames.CacheControl] = "no-cache"
    • 更新以反映变化。
    • 确保它高于其他 app.Uses(例如 app.UseDefaultFiles 或 app.UseStaticFiles()
    • @Aligned 谢谢,可能为我节省了半小时的调试时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2015-11-27
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多