Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Interview Questions: Show Post Cache Substitution

4.62/5 (6 votes)
7 May 2012CPOL2 min read 36.3K  
Show post cache substitution

This is one of the asked ASP.NET Interview Questions during the interview. Post cache substitution is used when we want to cache the whole page but also need some dynamic region inside that cached page. QuoteoftheDay, RandomPhotos, and AdRotator, etc. are examples where we can implement Post Cache Substitution.

Post-cache substitution can be achieved by two means:

Image 1

Figure: “Writesubstitution” in action

You can see that we have a static function here “GetDateToString()”. We pass the response substitution callback to the “WriteSubstitution” method. So now, when ASP.NET page framework retrieves the cached page, it automatically triggers your callback method to get the dynamic content. It then inserts your content into the cached HTML of the page. Even if your page has not been cached yet (for example, it's being rendered for the first time), ASP.NET still calls your callback in the same way to get the dynamic content. So you create a method that generates some dynamic content, and by doing so, you guarantee that your method is always called, and its content is never cached.

Image 2

<asp:substitution><asp:substitution>

Image 3

Figure: Substitution Control

Image 4

Figure: Substitution in Action

Below is a sample code that shows how substitution control works. We have ASPX code at the right hand side and class code at the behind code at the left hand side. We need to provide the method name in the “methodname” attribute of the substitution control.

View the following video on Web.config transformation in ASP.NET:

Image 5

For further reading do watch the below interview preparation videos and step by step video series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)