O'Reilly logo

Programming ASP.NET, 3rd Edition by Dan Hurwitz, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The HttpCachePolicy Class

Just as the OutputCache directive provides a high-level API for implementing caching, a low-level API is available through the HttpCachePolicy class. This class is contained within the System.Web namespace. It uses HTTP headers to control the caching. The HttpCachePolicy class mirrors the functionality provided by the page directive. It also provides additional low-level control, comparable to the type of control provided for object caching.

To use the HttpCachePolicy class to control output caching, do not include an OutputCache directive in the page file. Instead, use the Response.Cache syntax, as shown in the next example, OutputCacheLowLevel. Create this example by copying the OutputCaching example from early in this chapter, as shown in Figure 17-2.

The content file of OutputCacheLowLevel is functionally unchanged from the previous example, except for removing the OutputCache directive from the top of the file. If you leave the OutputCache directive in, it will override the calls using Response.Cache.

The code-behind file has two additional lines added to the Page_Load method, highlighted in Example 17-26.

Example 17-26. default.aspx.cs for OutputCacheLowLevel

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required