Matt Jenkins
Matt Jenkins

Reputation: 3081

Remove cache HTTP response headers from Web Api in Azure

I am trying to remove unwanted Cache-Control, Pragma and Expires HTTP headers in responses from a Web Api 2 project hosted on an Azure website in Standard mode.

I have tried the following in Global.asax Application_PreSendRequestHeaders:

var headers = ((HttpApplication)sender).Context.Response.Headers;
headers.Remove("Cache-Control");
headers.Remove("Pragma");
headers.Remove("Expires");

This works when debugging in Visual Studio. But on Azure, the headers are only removed for GET requests and not HEAD or POST requests.

Grateful for any suggestions!

Upvotes: 1

Views: 3040

Answers (1)

Mark Rendle
Mark Rendle

Reputation: 9414

Azure Web Sites supports the request filtering module, so you can do this in your web.config:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <remove name="Cache-Control" />
      <remove name="Pragma" />
      <remove name="Expires" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Disclaimer: I am not condoning the removal of these headers, which are an essential part of the HTTP protocol.

Removing cache headers says to clients "it is entirely up to you to decide how to cache this response", which may result in odd and hard-to-reproduce errors in production. If you want to disable caching, you should set these headers to values which explicitly disable caching:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Cache-Control" value="no-cache" />
      <add name="Pragma" value="no-cache" />
      <add name="Expires" value="-1" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Upvotes: 1

Related Questions