How to Implement Post-Cache Substitution using Substitution control and Response object

Dhirendra
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 8945 red flag

Post substitution cache wllows us to write dynamica content into web page if the page is served from cache.

Introduction


Post substitution cache allows us to write dynamic content into web page if the page is served from cache and we needed some part of the page should be refresh each time the page is requested by the user. In this case we can use the asp.net Substitution control or use response.WriteSubstitution method to render the certain part each time. In order to achieve this, we need to pass the static method to as a parameter to the HttpResponseSubstitutionCallback callback delegate of Response.WriteSubstitution

 object. The asp.net engine will not create any instance of the page if the page is serving through the asp.net cache rather it will call this callback static method if the response is serving though cache..


ASPX page content

<%@ Page Language="C#" AutoEventWireup="true"CodeBehind="CacheTestPage.aspx.cs"Inherits="WebApplication70515.CacheTestPage" %>

<%@ OutputCache Duration="10" VaryByParam="None" %>

<!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></title>

</head>

<body>

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

<div>

</div>

<br />

<asp:Substitution ID="Substitution1" MethodName="GetDateString" runat="server" />

<br />

<asp:Label ID="Label1" runat="server" Text="Get DateTime through Server cache"></asp:Label>:-

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<br />

<asp:Button ID="btnrefresh" runat="server" Text="Refresh" />

</form>

</body>

</html>

In the above code, we have added Substitution control and set static method name as "GetDateString" so that the asp.net will call this static method without creating instance of the page class. These static methods simily returns the date time string

ASPX.cs page's content

public partial class CacheTestPage : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label2.Text = DateTime.Now.ToString();

Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetDateTimeString));

}

public static string GetDateString(HttpContext context)

{

return "Date time using Substitution cache control <br>" + DateTime.Now.ToString();

}

public static string GetDateTimeString(HttpContext context)

{

return "DateTime through response object's write substitute cache method<br>" + DateTime.Now.ToString();

}

}


OutPut of the above code shown below screen shot

If we click second time on refresh button then the datetime on last will not refresh but the first two dates will be refresh using Post-Cache Substitution functionality.


 

Thanks for reading, do let me know your comment or feedback. Keep reading and sharing your knowledge! 

Conclusion


The above article show how we can use the Post-Cache Substitution functionlity in asp.net.

Page copy protected against web site content infringement by Copyscape

About the Author

Dhirendra
Full Name: Dhirendra Patil
Member Level:
Member Status: Member
Member Since: 3/23/2010 2:39:20 PM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Antoniacummins on: 3/21/2018 | Points: 25
We will utilize a server side regulator (replacement control) to execute present Cache replacement and on demonstrate the present time for each new Request while perusing alternate substance of the page from the yield Cache. you can visit if you want help https://24hwritemyessay.com .

Posted by: Ydv2125 on: 8/11/2018 | Points: 25
A great things are write on this article and he has impressed me. you can also impress to read this online tutorial http://windowstuts.net/binghistory it will be helping to remove past searched item in your bing web browser.

Login to post response

Comment using Facebook(Author doesn't get notification)