19

I've been searching for info on how to disable client side caching on project level. I know I can add the following before an action method:

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

I also read something about making profiles for caching, but that would also mean refering to them in several places. I would like a single setting in web.config, or maybe in IIS?

The project I'm working on contains a lot of partial views

Thank you in advance for any advice in this matter.

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
burktelefon
  • 988
  • 2
  • 8
  • 27

7 Answers7

40

You can disable browser caching via Web.Config:

<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Cache-Control" value="no-cache, no-store" />
                <add name="Pragma" value="no-cache" />
                <add name="Expires" value="-1" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

Source: http://blog.jamesjones.name/2009/11/how-to-disable-browser-caching-in.html

Edit: added no-store to Cache-Control for Chrome ( http://code.google.com/p/chromium/issues/detail?id=28035 )

You can set this at the project level or at the subdirectory level to control browser caching as desired. For example, in a primarily data-driven/dynamic site, I may set these headers at the project level, but in a /static directory (which contains my .js, .css, images), add another web.config which includes the appropriate </clear> directive, and perhaps set a far-future-expires header instead.

Tom
  • 1,977
  • 1
  • 20
  • 19
5

You could make BaseController and set your cache profile to it. Then make all of your controllers to inherit from this BaseController.


Update:

Here is what I've :

// Here is my custom OutputCaheAttribute to prevent cache at all.
//Whatever you may put anything you want.
//Of course i don't use it here but i put it to show you how it's going.
[NoCache]
public class BaseController : Controller
{
    protected override ViewResult View(string viewName, string masterName, object model)
    {
        // I do some stuffs here to change MasterPage depending on current culture.
        // Don't care about it i just wanna show you why BaseController is good idea.
    }
}

Then ALL my controllers inherits from this BaseController instead of normal Controller.

Hope this was helpful ;)

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • Thank you for the comment Wahid! But am I not forced to state one OutputCache-attribute for every ActionResult? Or can I do that on class-level somehow? – burktelefon Apr 01 '11 at 07:18
  • Yes you can do this at controller then all a Actions in this controller will take same cache settings. – Wahid Bitar Apr 09 '11 at 11:06
1

Expanding on @Tom's answer, for per file or per directory based cache busting :

<configuration>
<!--  disable cache for every file in this directory -->
  <location path="dist">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0" />
          <add name="Pragma" value="no-cache" />
          <add name="Expires" value="-1" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
</configuration>
Exlord
  • 5,009
  • 4
  • 31
  • 51
0

You can define cache profiles in web.config, however, using cache profiles doesn't seem to work with OutputCache attribute in mvc 3. Read this question: Caching ChildActions using cache profiles won't work?

Community
  • 1
  • 1
frennky
  • 12,581
  • 10
  • 47
  • 63
  • "doesn't seem to work" sounds a bit harsh. It sounds like it's just not as fully supported for child actions. – bzlm Mar 31 '11 at 21:53
  • @bzlm what's the difference? it can't be used for child actions. – frennky Mar 31 '11 at 22:39
  • Even if you remove everything except `Duration` and `VaryByParam` from the profile? – bzlm Apr 01 '11 at 07:15
  • @bzlm not sure i follow. name is required attribute so it can't be removed and removing enabled attribute doesn't help. – frennky Apr 01 '11 at 08:44
0

The OutputCache attribute is used for server side output action output caching. To turn it off, you just don't apply the attribute to the action/controller. If you want to disable client side, then that is taken care of by adding a header informing the browser not to cache the result.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
0

If you need to cache files in a subfolder for 1 day (24 hours), you can add a separate web.config to these sub folders (requires clearing client cache the first time).

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:24:00" />
        </staticContent>
    </system.webServer>
</configuration>
estinamir
  • 435
  • 5
  • 11
-1

Try this

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]

NoWar
  • 36,338
  • 80
  • 323
  • 498