简体   繁体   English

在ASP.net Web API中设置Owin Cache Control标头

[英]Setting Owin Cache Control headers in ASP.net Web API

I have the following code: 我有以下代码:

public class CacheHeader : OwinMiddleware
{
    public CacheHeader(OwinMiddleware next)
        : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
        context.Response.Headers["Pragma"] = "no-cache";
        context.Response.Headers["Expires"] = "0";
        await Next.Invoke(context);
    }
}

Which is supposed to have changed the Http cache control headers to have "no store, no-cache", but when I check it in Chrome Dev tools, I get the following: 应该将Http缓存控制标题更改为“no store,no-cache”,但是当我在Chrome Dev工具中检查它时,我得到以下内容:

Cache-Control:no-cache
Connection:keep-alive
Host:10.0.211.202
Pragma:no-cache

Is there a reason I'm not able to change what's in cache-control from no-cache to no-cache,no-store? 有没有理由我无法将缓存控制中的内容从无缓存更改为无缓存,无存储?

Make sure to introduce your middleware before registering web api: 在注册web api之前,请务必介绍您的中间件:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();

        app.Use((context, next) =>
        {
            context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
            context.Response.Headers["Pragma"] = "no-cache";
            context.Response.Headers["Expires"] = "0";

            return next.Invoke();
        });

        app.UseWebApi(config);
    }
}

Just a guess. 只是一个猜测。 Have you tried to set the response headers after next.Invoke()? 您是否尝试在next.Invoke()之后设置响应标头?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM