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 ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.