Outputcache - How to not cache Master Page

Hi there

We are using outputcache on a couple of our pages - <%@ OutputCache Duration=“300” Location=“Server” VaryByParam=“None” %>

The problem is that our master page has some dynamic controls loaded onto it that we do not want to cache. Is there any way to cache everything except the master page? I know you can partially cache user controls-the varybycontrols parameter–isnt the master page a child control of your page?

Is there any easy way to do this?
Any help would be greatly appreciated.

Ok I found an interesting article --I can use the substitution technique to display the correct logged in user on my master–but it is very limited-only returns a string and you can only access the HttpContext–I have login boxes for example that dissapears when you are logged in…so I need to hide/show controls dynamically.

What i can do is put those logon controls in a user control and then just disable cache for that specific control. Many sites provide this as the method -<%@ OutputCache Duration=“0” VaryByParam=“none” %> --but this give me a compiler error–duration must be set to a positive integer value… also by putting Response.Cache.SetCacheability(HttpCacheability.NoCache); in the code behind of the .ascx removes all caching on the page…

Any idea on how to disable caching on a specific user control?

I’m not sure how can you prevent the user control to be cached, but you can remove it from the output cache using HttpResponse.RemoveOutputCacheItem

I have a similar problem where I want to cache my page, but the page has a control from the AJAX Control Toolkit on it (the RoundedCornersExtender) which breaks when a cached version of the page is displayed. I was going to put this control in its own user-control which wasn’t cached, but this won’t work

Seems that if your page isn’t cached you CAN cache a portion of the page if you put it in a user control. Contrarily, when you cache a page, everything gets cached and you can’t turn caching off for a particular user control.

@opiator: If you want to show alternative output depending on states, the pages are not cacheable. However, if it is important you still have some options.

But first you have to realize that despite its name, the master page is not really a page, thus it cannot be cached like a normal page. ASP.NET plays a trick on you: A master page is in reality a top-level control on the content pages. When you use output caching on a content page, the entire page including the output generated by the “master page” control (the content controls being children of this) is output cached.

If your “problem” is just the login status you can use a regular form instead of asp.net server side controls and use ASP.NET AJAX for logging in. ASP.NET AJAX does actually have an API specifically for authentication through javascript.

If you don’t like being dependant on javascript, you can still make the cache depend on the logged-in “state”. To do this you have to use a “VaryByCustom” scheme. This allows you to vary page caches depending on a number of parameters/states. Read up on “VaryByCustom”.

@pinch: IMHO the RoundedCornersExtender is one of the most useless controls of the toolkit. You are better off designing the corners yourself and using markup/CSS to place them. You are correct that when you can use output caching on both control and page level. When you use output caching both pages and controls always cache all of their output, including child controls. Except, you can use a special substitution control which will be replaced by a string from a static page method.

The best way to cache is to cache the entire page, if possible. That will also let downstream caches do the caching, i.e. the browser itself. That can really safe you requests. If that’s not possible you can perhaps get by using substitutions. Or you can accept that the page is uncacheable but maybe parts of the page can be cached, typically “expensive” parts - in terms of computation/latency.

@snomag-the HttpResponse.RemoveOutputCacheItem only works with aspx pages and not with ascx controls…

@honeymonster I have used VaryByCustom and the following techinque to remove it from cache

if (Cache[“masterCache”] == null)
{
Cache.Insert(“masterCache”, “”);
}
PartialCachingControl pc = (PartialCachingControl)LoadControl(“/profiles/ControlPanel.ascx”);
pc.Dependency = new System.Web.Caching.CacheDependency(null, new string { “masterCache” });
placeHolderTest.Controls.Add(pc);
Cache.Remove(“masterCache”);

These 2 techniques works—but only if I only have output cache on the control and not on the page…When i put output cache on my page—it overrides anything i try to do on the user controls…

@pinch “Seems that if your page isn’t cached you CAN cache a portion of the page if you put it in a user control. Contrarily, when you cache a page, everything gets cached and you can’t turn caching off for a particular user control.” --you seem to be right :frowning:

I honestly cannot believe that there isnt a way to override the main pages’s outputcache in a user control…anyone?My only option left seems to be substitution control technique…but our master page has a lot of custom stuff–each user has a different backgroud,menu etc…so using the substitution technique would be a huge mission…but will have a look