ASP.NET: Controlling Caching in ASP.NET Web Forms


Previous Topic Previous Next Topic Next
Xoc Software
Training
RVBA Conventions
Maya Calendar Program
Company Information
Tools
ASP.NET and Other Tips
.NET: Debugging Designer Features of a Custom Control in Visual Studio
.NET: Setting the Default Font in a Windows Mobile/Compact Framework Custom Control
.NET Fixing C# XML Comments so they Work in Windows XP SP2
.NET: Getting and Setting the Application Version Number
.NET: Getting the Path of the Executing Assembly
.NET: Retrieving Assembly Attributes
.NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET
.NET: Sizing Columns in a ListView Control in .NET
.NET: Using Remoting in .NET
ASP.NET: Constructing a Graphic on the Fly in ASP.NET
ASP.NET: Controlling Caching in ASP.NET Web Forms
ASP.NET: How to use the FrontPage Server Extensions with ASP.NET
ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms
ASP.NET: Using Forms Authentication in ASP.NET
ASP.NET: View Trace Information on your ASP.NET Web Pages
ASP: Create XML from an ADO query
ASP: Detect Incomplete Loads
ASP: Including an ASP.NET Web Page In a Classic ASP Web Page
ASP: Process .HTM Files with Scripts and Server Side Includes
ASP: QuickSort Algorithm
ASP: Retrieve all server variables from IIS
ASP: Send Email from Active Server Page
HTML: How to Create a Non-Scrolling Region in HTML
IE: Allowing Only Certain ActiveX Controls to Run in Internet Explorer
IIS: Creating a web site for testing in IIS Server
IIS: Creating Multiple Web Sites within IIS on Windows 2000 and Windows XP Professional
IIS: IIS/Visual InterDev Problems and Fixes
IIS: Redirect a domain such as xoc.net to www.xoc.net
SQL Server: Execute SQL Server Updategram
Web Design: Design for People with Disabilities
Web Design: Keep a Web Page out of the Google Cache
Windows: Get HTTP Header of a Web Page using Telnet
Windows: Testing Domain Names without DNS
Windows: Using Hosts File to Access Web Sites with XP SP2
Windows: Windows XP Command Line Tools
Windows Mobile: Reprogramming the Push-to-Talk Button on the AT&T Tilt
Articles
Miscellaneous
Downloads
Links
Search
Email

Other Xoc managed sites:
http://grr.xoc.net
http://www.986faq.com
http://www.mayainfo.org
https://mayacalendar.xoc.net
http://www.yachtslog.com

ASP.NET allows you to cache pages. The means that when the code behind the page runs, it produces HTML, the HTML is sent down to the client, however a copy is stored in the memory of the web server. If the same page is requested again, then the page is retrieved from the cache and the code is not rerun. You have virtually infinite flexibility on controlling when the cache gets flushed.

The most basic caching is implemented by placing this line at the top of your ASPX page:

<%@ OutputCache Duration="3600" VaryByParam="none"%>

What this tells the ASP.NET caching code is to cache the page for one hour.

The full spec for the @OutputCache lines is:

<%@ OutputCache Duration="#ofseconds" Location="Any ¦ Client ¦ Downstream ¦ 
Server ¦ None" VaryByControl="controlname" VaryByCustom="browser ¦ 
customstring" VaryByHeader="headers" VaryByParam="parametername" %>

Duration is a count in seconds to cache.

Location allows the caching to occur on the server, on the client, on a proxy server in between. The default is Any. If you always want server caching (which seems to me to be the most useful choice), change the line to read:

<%@ OutputCache Duration="3600" Location="Server" VaryByParam="none"%>

VaryByControl is only used by user controls, not on standard web pages. See the .NET documentation for more details.

VaryByCustom="browser" keeps a different copy of the output for each browser name and major version information. So if you have cloaking by browser version going on (which is easy to implement in .NET), then each separate page will get delivered.

VaryByCustom="customstring" Allows you to specify a string that will be passed to your code. To make this useful, you must then override the GetVaryByCustomString method in the Global.asax file. For example, place this line in your ASPX file:

<%@ OutputCache Duration="3600" Location="Server" VaryByCustom="Referer" VaryByParam="none"%>

Then in your Global.asax file add the following code:

public override String GetVaryByCustomString(System.Web.HttpContext hcContext, String strCustom) 
{ 
    switch (strCustom) 
    { 
    case "Referer": 
        Uri uriReferrer = hcContext.Request.UrlReferrer; 
        String strRet; 
        if (uriReferrer != null) 
            strRet = uriReferrer.Host; 
        else 
            strRet = null; 
        return strRet; 
    default: 
        return base.GetVaryByCustomString(hcContext, strCustom); 
    } 
} 

VaryByHeader allows you to cache based off of some field in the HTTP header sent by the client. The classic example is based off the Accept-Language header line.

VaryByParam allows you to cache different versions based off of querystring or post field parameters. So http://www.domain.com/foo.aspx?bar=baz would be cached separately from http://www.domain.com/foo.aspx?bar=bletch

There are also ways of controlling the caching through code.


Top