8

I am trying to implement static files cache in the ASP.net mvc application.

What i did:

I've added into Content folder a web.config file with the following content:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="300.0:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

In the website web.config file, i commented out some lines of code:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <!-- Commented them out
      <add verb="GET" path="*.js" name="Static for js" type="System.Web.StaticFileHandler" />
      <add verb="GET" path="*.css" name="Static for css" type="System.Web.StaticFileHandler" />
      <add verb="GET" path="*.png" name="Static for png" type="System.Web.StaticFileHandler" />
      <add verb="GET" path="*.jpg" name="Static for jpg" type="System.Web.StaticFileHandler" />
      -->
    </handlers>

Now, publishing the website and inspecting the resources, i get the folowing response:

enter image description here

I see is missing the Expire header ?! (shouldn't it be there in order for cache to work)

Does the response headers tell the browser to cache the resource for the next 25920000 seconds?

I am doing things correctly in order to cache the static files?

Catalin
  • 11,503
  • 19
  • 74
  • 147

1 Answers1

6

This is correct, and you should be fine.

Some might say you should add Expires, too, for clients which do not understand HTTP/1.1, but as already argued in the first linked article, that shouldn't be a real concern, even less now, 7 years later.

Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
  • Ok, so as long as i have `Cache-Control: public, max-age=xxxxx` means the browser will cache the resource – Catalin Feb 04 '14 at 12:27
  • @RaraituL i have the same issue, i was missing the expire header, and google speed test says add expires for images, css, js. did you find any solution ? – Shaiju T Mar 16 '16 at 07:49
  • @stom sorry, i just read this comment. I think it was working with `Cache-Control` header – Catalin Jan 23 '18 at 09:43