3

I'm looking to add expire headers to my website so I can set a cache time for the files.

I've found the following the example, but I'd like to set it to only cache JPG, PNG, GIF, JS and CSS files if possible?

<system.webServer>
    <staticContent>
        <clientCache cacheControlMaxAge="14.00:00:00" cacheControlMode="UseMaxAge"/>
    </staticContent>
</system.webServer>

Thanks for any help!

4 Answers4

5

What you can do is to create web.config files in the folders where your files are. (You might have folder such "Images" for images and "Js" for javascript files, "Css" for style sheets... etc.) Then, you paste yor code in those files. By doing that you apply your cache settigs to all files in those folders, regardless of the file type. This is more flexible method than applying cache settings to a particular file extension.

walterD
  • 51
  • 1
  • 2
3

IIS does NOT support dynamic expires headers for static content.

You can add a static expires header this way:

<system.webServer>
  <staticContent>
      <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
  </staticContent>
</system.webServer>

Source: The Official Microsoft IIS site

There is a similar question here: IIS 7.5 How do you add a Dynamic HTTP Expires Header

Community
  • 1
  • 1
Balazs Nemeth
  • 497
  • 4
  • 3
1

As already pointed out using another web.config in the specific folder is probably the best option.

However you can override the cache control header with and outbound rewrite rule:

<system.webServer>
...
<rewrite>
  <outboundRules>
    <rule name="RewriteCacheControlForHTMLFiles" preCondition="jsFile">
        <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
        <action type="Rewrite" value="max-age=86400" />
    </rule>
    <preConditions>
        <preCondition name="jsFile">
            <add input="{REQUEST_FILENAME}" pattern="\.js$" />
        </preCondition>
    </preConditions>
  </outboundRules>
  ...
zapdev
  • 161
  • 3
  • 10
0

If you want to cache specific Items I would go the route of doing this programatically. You could use the following code to get you going. It's from microsoft, i just brought it over so you don't have to go find it. http://msdn.microsoft.com/en-us/library/ff477235.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Caching;
using System.IO;

public partial class _Default : System.Web.UI.Page
{


    protected void Button1_Click1(object sender, EventArgs e)
    {
        ObjectCache cache = MemoryCache.Default;
        string fileContents = cache["filecontents"] as string;

        if (fileContents == null)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration =
                DateTimeOffset.Now.AddSeconds(10.0);

            List<string> filePaths = new List<string>();
            string cachedFilePath = Server.MapPath("~") +
                "\\cacheText.txt";

            filePaths.Add(cachedFilePath);

            policy.ChangeMonitors.Add(new
                HostFileChangeMonitor(filePaths));

            // Fetch the file contents.
            fileContents = File.ReadAllText(cachedFilePath) + "\n"
                + DateTime.Now.ToString();

            cache.Set("filecontents", fileContents, policy);

        }

        Label1.Text = fileContents;
    }
}
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33