4

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?

1
  • Did you find a solution do this? Commented Jul 18, 2016 at 16:08

2 Answers 2

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.