How do I disable caching of an individual file in IIS 7 using weserver config settings

孤者浪人 提交于 2019-12-28 05:36:30

问题


Is there any way to diable the caching of a single javascript file in my ASP.NET applicaiton without disabling the caching of any other files in the application?

It is running on IIS 7 in Azure, so to me it seems that my only options of controlling this are within the webserver tags.

I currently use the folowwing config, but this is disabling cache on all files.

     <modules runAllManagedModulesForAllRequests="true"/>

    <staticContent>
      <clientCache cacheControlMode="DisableCache"/>
    </staticContent>

  </system.webServer>

I just want to disable cache of a single javascript file that changes quite often.

Is it possible?


回答1:


I just stumbled across this question; you can use the following to disable the cache on a specific file:

<configuration>
  <location path="path/to/the/file">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
  </location>
</configuration>

(Note that the path is relative to the web.config file)

Alternatively, place the single file in a directory on it's own, and give that directory it's own web.config that disables caching for everything in it;

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

[Both tested on IIS7.5 on Windows 7, but you'll have to confirm that it works OK on Azure]




回答2:


You're going to want to look at the System.WebServer/Caching class, where you can apply a caching profile to particular extensions. This will at least enable you to control it for all Javascript files ending with the .js.

<system.webServer>
...

   <caching>
      <add extension=".js" policy="DontCache" kernelCachePolicy="DontCache" />
   </caching>

</system.webServer>

That should disable .js caching on both process and kernel caching from the cloud.

I think you can create this web.config in a folder that contains just your file and it will disable caching for .js only at that folder level. I honestly have not tried that myself, so just a suggestion you could test.


Beyond that, take a look at the documentation for IIS related to caching config:

/Caching: http://www.iis.net/ConfigReference/system.webServer/caching

/Caching/Profiles: http://www.iis.net/ConfigReference/system.webServer/caching/profiles

/Caching/Profiles/Add: http://www.iis.net/ConfigReference/system.webServer/caching/profiles/add

Hopefully that, plus some research on those config tags will help.

If not, I'd recommend looking into implementing a custom HTTP Module that you can insert into the IIS request pipe, which can filter your caching control down to that particular file

** for what its worth this is just IIS behavior and won't be different in or out of Azure, so you could easily test this local without bothering with the Dev fabric or Azure testing.




回答3:


Looks like the above answer is missing a "profiles" tag

<caching>
  <profiles>
    <add extension=".js" kernelCachePolicy="DontCache" policy="DontCache"/>
  </profiles>
</caching>



回答4:


You can disable output caching on folder level by removing an extension on folder level by including a web.config in that folder like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
        <caching>
            <profiles>
                <remove extension=".js" />
            </profiles>
        </caching>

  </system.webServer>
</configuration>

now this folder will not have output caching enabled for files ending with .js




回答5:


Open IIS panel.

Open the related application from sites, then navigate to folder containing particular file.

Now from right pane, switch to content view

Select your desired file

Now right click on file to choose "Switch to Feature View".

From there choose HTTP Response Headers, click "Add" to add new header, Add "Cache-Control" header with "no-cache" value.




回答6:


Yes, it is possible, you can do it in the configuration file:
(See https://stackoverflow.com/a/4821328/2247494)

<configuration>
    <location path="cache.manifest">
      <system.webServer>
        <staticContent>
          <clientCache cacheControlMode="DisableCache" />
        </staticContent>
      </system.webServer>
    </location>
</configuration>


来源:https://stackoverflow.com/questions/3929284/how-do-i-disable-caching-of-an-individual-file-in-iis-7-using-weserver-config-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!