Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [403]

By Root 5227 0
Pages


If you plan to cache user controls—that is, if you’re trying for partial caching—it’s probably because you just don’t want to, or cannot, cache the entire page. However, a good question to ask is this: What happens if user controls are cached within a cacheable page?

Both the page and the controls are cached individually, meaning that both the page’s raw response and the control’s raw responses are cached. However, if the cache duration is different, the page duration wins and user controls are refreshed only when the page is refreshed.

A cacheable user control can be embedded both in a cacheable page and in a wrapper-cacheable user control.

Important

Cacheable user controls should be handled with extreme care in the page’s code. Unlike regular controls, a user control marked with the @OutputCache directive is not guaranteed to be there when your code tries to access it. If the user control is retrieved from the cache, the property that references it in the code-behind page class is just null.

if (CustomerGrid1 != null)

CustomerGrid1.Country = "USA";

To avoid bad surprises, you should always check the control reference against the null value before executing any code.

Advanced Caching Features


The output caching subsystem has also a few other cool features to offer. They are caching profiles and post-cache substitution. In brief, caching profiles let you save a block of output caching-related settings to the configuration file. Post-cache substitution completes the ASP.NET offering as far as output caching is concerned. In addition to saving the entire page or only fragments of the page, you can now also cache the entire page except for a few regions.

Caching Profiles


The @OutputCache directive for pages supports the CacheProfile string attribute, which references an entry under the section in the web.config file:

location="..." sqlDependency="..."

varyByCustom="..." varyByControl="..."

varyByHeader="..." varyByParam="..."

noStore=true|false"

/>

Basically, by defining a named entry in the section you can store in the configuration file all the cache-related settings to be applied to the page. Instead of specifying the same set of parameters for each page over and over again, you can put them in the web.config file and reference them by name. In this way, you can also modify settings for a number of pages without touching the source files.

<%@ OutputCache CacheProfile="MySettings" %>

In the preceding code, the application has a MySettings entry in the section and doesn’t need any additional attribute in the @OutputCache directive. As you can see, the attributes of the node match the attributes of the @OutputCache directive.

Post-Cache Substitution


With user controls, you can cache only certain portions of ASP.NET pages. With post-cache substitution, you can cache the whole page except specific regions. For example, using this mechanism, an AdRotator control can serve a different advertisement on each request even if the host page is cached.

To use post-cache substitution, you place a new control—the control—at the page location where content should be substituted, and you set the MethodName property of the control to a callback method. Here’s a quick example:

The output you see has been generated at:

<%=DateTime.Now.ToString() %> and is valid for 30 seconds


This content is updated regularly

MethodName="WriteTimeStamp" />


This is more static and cached content

The MethodName property must be set to the name of a static method that can be encapsulated in an HttpResponseSubstitutionCallback delegate, as follows:

public static string WriteTimeStamp(HttpContext

Return Main Page Previous Page Next Page

®Online Book Reader