Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable client side caching

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.

like image 420
burktelefon Avatar asked Mar 31 '11 07:03

burktelefon


People also ask

How to disable browser caching in web config?

If the folder already has a web. config file, edit the existing file to include <clientCache cacheControlMode="DisableCache" /> within the staticContent element.

Can I disable DNS cache?

To turn off DNS caching for a particular session, type net stop dnscache and hit Enter. To turn on DNS caching, type net start dnscache and hit Enter.

Which middleware will prevent client caching site resources which are old?

Pragma: no-cache.

Is cache stored client side?

Is it client side or server side? It can be either, or both. Server side caches are generally used to avoid making expensive database operations repeatedly to serve up the same content to lots of different clients.


2 Answers

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.

like image 58
Tom Avatar answered Dec 23 '22 05:12

Tom


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 ;)

like image 35
Wahid Bitar Avatar answered Dec 23 '22 04:12

Wahid Bitar