2

So I have got an ASP.NET MVC 5 site running on IIS 10 locally on my Windows 10 laptop, with the following settings in web.config.

Compiled in Release configuration, debug mode off:

<compilation debug="false" targetFramework="4.5.2" />

and a Cache-Control header using max-age:

<staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>

But no matter what I do, I always see Cache-Control: private

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8

I've turned off all modules I had and ran a failed trace request, which showed the header is being set:

enter image description here

But I can't track down anything about this ManagedPipelineHandler which appears to be the module in which the header is set.

I tried adding Cache-Control as a custom header:

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
    <remove name="Cache-Control" />
    <add name="Cache-Control" value="public, max-age=1800, must-revalidate" />
  </customHeaders>
</httpProtocol>

But this only appended onto the existing private setting:

HTTP/1.1 200 OK
Cache-Control: private,public, max-age=1800, must-revalidate
Content-Type: text/html; charset=utf-8

My runAllManagedModulesForAllRequests setting is false which other posts have suggested might be the cause.

I know I can set this header in code but I'd love to know what & why is forcing the "private" setting.

Can anyone advise?

UPDATE

Something is forcing responses to have cache-control: private in IIS7

So this is default behaviour for .NET when there's no output cache used for a request (and you have output cache enabled). If you set the sendCacheControlHeader attribute to false for & in web.config - you don't get the Cache-Control: private header!

Oddly you still don't get the correct Cache-Control header based on the node - but you control the header via the section, so this now works:

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
    <add name="Cache-Control" value="max-age=30,public" />
  </customHeaders>
</httpProtocol>
Neil
  • 2,618
  • 1
  • 21
  • 31
  • Were you able to solve this problem? I am having the same issue. I want to return different cache control headers for WebApi methods so I can cache results on CDN. – DavidJBerman Jan 08 '21 at 21:31

1 Answers1

1

Caching in this context is being controlled by .NET and MVC application, which is what is driving the "private" value. To control this value, you usually would decorate controllers or actions with appropriate caching behavior:

  • [OutputCache(...)] > Specifically control the output cache control such as duration in seconds, variances by encoding, parameter, header or other value, location, or other configuration options

If no cache value is set, the "private" value is the default used by MVC. If you want to apply a cache setting globally, then you can create a filter and apply it in the global filters during application initialization.

Jason W
  • 12,449
  • 2
  • 24
  • 57
  • 1
    Are you saying then that the "default" MVC setting overrides the perfectly legitimate system.webserver setting? That just doesnt seem right. – Neil Aug 09 '17 at 08:18