4
votes

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:

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?

2
Did you find a solution do this? - Sujesh Arukil

2 Answers

2
votes

Make sure to introduce your middleware before registering 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);
    }
}
0
votes

Just a guess. Have you tried to set the response headers after next.Invoke()?