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:
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.
<asp:substitution><asp:substitution>
Figure: Substitution Control
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:
For further reading do watch the below interview preparation videos and step by step video series.