locked
Header Says no-cache Despite Output Caching Enabled with Proper Headers RRS feed

  • Question

  • User-939035612 posted

    All the js, css, and image files on my site are being rendered with the following header'

    cache-control: no-cache,max-age=2592000,public

    My web.config settings for headers and caching looks like this:

    <customHeaders>
            <remove name="X-Powered-By" />
            <remove name="X-SourceFiles" />
            <add name="X-Xss-Protection" value="1; mode=block" />
            <add name="X-Content-Type-Options" value="nosniff" />
            <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload" />
            <add name="X-Permitted-Cross-Domain-Policies" value="none" />
            <add name="X-Frame-Options" value="sameorigin" />
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="access-control-allow-headers" value="content-type" />
            <add name="Referrer-Policy" value="same-origin" />
            <add name="Cache-Control" value="public" />
          </customHeaders>
    
    
    ...
    
     <modules>
          <remove name="AspNetCoreModuleV2"/>
          <add name="UriCacheModule"/>
          <add name="FileCacheModule"/>
        </modules>
        <staticContent>
          <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
        </staticContent>
        <caching enabled="true" enableKernelCache="true">
          <profiles>
            <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".pdf" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".svg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".json" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" varyByQueryString="*" location="Any" />
            <add extension=".aspx" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
          </profiles>
        </caching>
    

    How is it possible for my headers to say "no-cache" when my cache control header says "public"?

    On the server I have output caching enabled so please respond to this with directions for enabling output caching in IIS. 

    UPDATE: This problem appears unique to this site and not other WebForms sites on the same server. There is one other site with this problem. It is a .Net Core site that had a header saying simply "no-cache" but when I tried changing it to public in the Http Headers section of IIS it only appended public, so now it says "no-cache public" What the sites without the problem have in common is that they were originally built with .Net 2.0 whereas the new sites were created with .Net 4.0, .Net Core 3 or later.

    The application does not have this problem on localhost so I think it must be something on the server configuration, but also read on an IIS forum where a guy had some add on that was automatically doing it to him. In my case it would obviously be one that does it on the remote server but not my development machine. I have AjaxControlToolkit, BotDetect Captcha, and System.Web.Optimization.

    I tried removing the cache control header in the Application_PreSendRequestHeaders section of my Global.asax file as follows:

    HttpContext.Current.Response.Headers.Remove("Cache-Control");
            HttpContext.Current.Response.Headers.Add("Cache-Control", "public");

    This appears to be working, but I would like to know if there are any downsides. There still seem to be instances where a max age is appended.

    Monday, January 18, 2021 7:41 AM

All replies

  • User1535942433 posted

    Hi CopBlaster,

    Accroding to your description,why you need to use 'public'? Between your Web server and a user requesting your page, there may be proxy servers configured to cache Web pages for faster response times. Usually ASP pages are developed to be unique for each user or may contain secure information. For this reason, IIS sets this property to "Private" so that proxy servers or other cache mechanisms will not cache pages. 

    Setting CacheControl to "Public" may seem to improve the performance of your .asp files, but it is discouraged if you generate custom HTML for every request, and a proxy server might interfere with the response.

    If you need to append maxage,you could do this:

    HttpContext.Response.Headers.Append("Cache-Control", "public,max-age=31104000");

    Best regards,

    Yijing Sun

    Tuesday, January 19, 2021 5:16 AM