Output Cache with VaryByParam in ASP.NET

 

Whenever you are creating Master/Details page in asp.net, if you want to cache the details page based on orderId by using asp.net output cache, we can do it easily by using VaryByParam option. For example, if we are passing the orderId to details page from master page and to cache each order details use VaryByParam option as shown below.

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Details.aspx.cs" Inherits="_Default" %>

 

<%@ OutputCache Duration="60" VaryByParam="orderId" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

 

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>Order Details Page</title>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        <asp:GridView ID="gvOrderDetails" runat="server"></asp:GridView>

 

    </div>

 

    </form>

 

</body>

 

</html>

 

 

As shown above asp.net engine will cache the each order details page for 60 seconds. That means if we have 100 orders, asp.net engine caches the 100 orders for each order it will cache one copy.