0

I guess there must be some kinda HTTP-header code controlling the cache in the browser, but how do I set this "time" from c# using Razor-engine syntax?

OutputCache duration ? is that usable somehow?

Would like to minimize bandwidth on huges sites that doesnt chance certain javascripts and css all the time, so a cache time of eg. 30 days or more would be very usable + I would like to be able to specify this per file from serverside.

BerggreenDK
  • 4,915
  • 9
  • 39
  • 61

2 Answers2

0

You can configure IIS to cache static content in your web.config. See this question for an example:

<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>
Community
  • 1
  • 1
Chris Van Opstal
  • 36,423
  • 9
  • 73
  • 90
  • good links, the web.config file isnt really a solution for me, I would rather code a handler of some sort that will return the correct .css and .js based upon the user and that file should be cached if possible. I believe there are some "http response headers" I can use for that, and I was hoping to see some examples written in C# and perhaps implemented with Razor syntax. Hope this makes sense. – BerggreenDK Apr 17 '13 at 21:29
0

One option is to break down your files into different folders (either based on caching desire or simply by type). Once you've done that, you can implement caching at a directory level.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>
  </system.webServer>
</configuration>

The default value (if not specified) for cacheControlMaxAge from the IIS docs:

The default value is 1.00:00:00 (1 day)

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • is there a programatic way of doing it for a single file ad hoc? like if I write my own "service/handler" for special .js or .css files? Some of them will include user specific setups for colors, sizes, certain lists of text and numbers and these would fill up quite a lot of space if placed in the folder for caching. I would like to dynamically generate it on the fly if possible.. and then have the cache keeping it until they need it again. Secondly, I would need some kinda of "expire" on the files if I have to generate them physically. – BerggreenDK Apr 17 '13 at 14:08
  • @BerggreenDK - I don't believe you can do this out of the box for a single file (it's always at directory level by default). You could write an HTTP handler and then have code setup to look for certain files and adjust the HTTP header there. There's a lot of information available about creating those handlers. However, you would want to test to make sure that IIS isn't altering any of your headers later in the pipeline. – Justin Helgerson Apr 17 '13 at 18:11