I'm working on an ASP.NET web application and am facing a challenge in removing or altering the "Cache-Control" and "Pragma" headers in the HTTP response. I tried to do this in my OWIN middleware as follows:
app.Use(async (context, next) =>
{
await next.Invoke();
context.Response.Headers.Remove("Cache-Control");
context.Response.Headers.Remove("Pragma");
});
But even after this, when I inspect the response headers in Postman, I still see the "Cache-Control" and "Pragma" headers with their original values.
I've searched through my entire solution and can't find any instance where these headers are being set. I suspect they're being added automatically somewhere outside of my code, but I'm not sure where.
Does anyone have any suggestions on where else these headers might be coming from or how I can override or remove them?
You shouln't modify response header once the response has started,it would result in an error as below(You could check if the response has statred with HttpContext.Response.HasStarted
):
The service and middleware relaed with Cache-Control/Pragma header,try remove them :
builder.Services.AddResponseCaching();
.....
app.UseResponseCaching();
Here's the related document