简体   繁体   中英

Cannot get ASP.NET/IIS to serve .js files with custom cache-control settings

In my ASP.NET MVC 5 app, I have used the clientCache web.config attribute to customize the caching behavior for static files.

<system.webServer>
  <staticContent>
    <clientCache cacheControlCustom="private,max-age-300" setEtag="true" />
  </staticContent>
</system.webServer>

This is working fine for .css and image files, but I'm noticing in my browser's dev tools that .js files are not getting the custom cache-control and etag headers that the other file types are getting.

在此处输入图片说明

In addition, I've tried adding a custom handler, but it hasn't had any effect from what I can tell.

<handlers>
  <add name="StaticHandler_js" verb="*" path="*.js" type="System.Web.StaticFileHandler" />
</handlers>

Any ideas on how I can get ASP.NET/IIS to treat .js files the same way as other static files?

<system.webServer>  
    <staticContent>  
      <clear/>  
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:01:00" />  
      <mimeMap fileExtension=".jpg" mimeType="image/jpg"/>  
      <mimeMap fileExtension=".png" mimeType="image/jpg"/>  
      <mimeMap fileExtension=".css" mimeType="text/css"/>  
      <mimeMap fileExtension=".js" mimeType="text/javascript"/>  
    </staticContent>  
    <validation validateIntegratedModeConfiguration="false" />  
</system.webServer>  

I was able to get around this by adding path-specific configuration to clear out all handlers and add only the static file defaults to those paths. This is an imperfect solution because it's based on the file path, not the file type , but because all of my JavaScript files are in this singular folder, it does the job.

<location path="Scripts">
  <system.webServer>
    <handlers>
      <clear />
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read"/>
    </handlers>
  </system.webServer>
</location>

If anyone has a better solution that doesn't rely on file path, I'll gladly accept that as the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM