Output Caching with IIS7 – How To for an dynamic aspx page

asp.netcacheiis-7

I have a RetrieveBlob.aspx that gets some query string variables and returns an asset. Eeach url corresponds to a unique asset.

In the RetrieveBlob.aspx a Cache Profile is set. In Web.Config the profile looks like (under system.web tag:

<caching>
  <outputCache enableOutputCache="true" />
  <outputCacheSettings>
    <outputCacheProfiles>
      <add duration="14800" enabled="true" varyByParam="*" 
           name="AssetCacheProfile" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

Ok, this works fine. When I put a breakpoint in the code behind of RetrieveBlob.aspx, it gets triggered the first time, and all the other times not.

Now, I throw away the Cache Profile and instead I'm having this in my Web.Config under System.WebServer:

<caching>
  <profiles>
    <add extension=".swf" policy="CacheForTimePeriod"
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".flv" policy="CacheForTimePeriod" 
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".gif" policy="CacheForTimePeriod" 
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".png" policy="CacheForTimePeriod"    
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".mp3" policy="CacheForTimePeriod" 
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".jpeg" policy="CacheForTimePeriod" 
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
    <add extension=".jpg" policy="CacheForTimePeriod" 
         kernelCachePolicy="CacheForTimePeriod" duration="00:08:00" />
  </profiles>
</caching>

Now the caching doesn't work anymore. What am I doing wrong? Is it possible to configure under Caching tag of System.WebServer a Caching Profile for a Dynamic aspx page?

I already tried adding something like this:

<add extension="RetrieveBlob.aspx" policy="CacheForTimePeriod" 
     kernelCachePolicy="CacheForTimePeriod" duration="00:00:30" 
     varyByQueryString="assetId, assetFileId" />

But it doesn't work.

An example of an url is:

http://{server}/{application}/trunk/RetrieveBlob.aspx?assetId=31809&assetFileId=11829

Best Answer

The <caching> tag that you have enabled in web.config under system.web is .net specific and IIS 7 will not address that caching content. Now, the <caching> you has been configured in system.webServer is handled by IIS 7 module and should work.

If you are looking to enable caching for a specific page like RetrieveBlob.aspx, you will need to add it under <location> tag e.g.:

<location path="RetrieveBlob.aspx">
    <system.webServer>
        <caching>
            <profiles>
                <add extension=".aspx" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="00:00:30" varyByQueryString="assetId, assetFileId" />
            </profiles>
        </caching>
    </system.webServer>
</location>

The easiest way to get this done using the IIS 7 UI. Here is my blog which talks about File level authentication in IIS 7, but you can do the same for Output Caching.

Go the Web Site (Default Web Site in our case)
Click Content View
Right click on the file e.g. RetrieveBlob.aspx -> Switch to Features View
Double click Output Caching
Now, click Add... and make the necessary caching changes