Using Microsoft Visual Studio
Page 4 - Dynamically Update Portions of Cached Web Pages with Post-Cache Substitution 2006-10-14
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 4 of 4 )
-World Scenario">
A Real World Scenario: Dynamic Membership Status on a Cached Page
The output cache substitution fixed a problem that I encountered with the ASP.NET 2.0 Membership controls.
I have a Web site that contains some static pages. The site also has membership functionality. It uses the new ASP.NET 2.0 LoginStatus and LoginName controls to display information about the current user. Even though the page content doesn't change, these controls need to display different information. When the user is not logged in, the status control displays a hyperlink that says "Please Login," and the LoginName is empty. When she is logged in, the status has a hyperlink with the text "Logout," and the LoginName displays, for example, "Julie Lerman."
When I had caching turned on for these pages, the LoginName and LoginStatus controls were not displaying the accurate information — they were drawing the HTML from the output cache.
One solution would have been to turn off the output cache and to re-render those pages every single time they were requested. For this particular site, which does not receive heavy traffic, that might have been an acceptable solution. However, being bull-headed, I wanted to find a proper solution to the problem. Then, the next time, when site performance is a concern, I'd know just what to do.
I replaced the LoginName control and the LoginStatus controls with inline code, using the Response.WriteSubsitution method.
<% Response.WriteSubstitution(New HttpResponseSubstitutionCallback(AddressOf _ GetLoginStatus))%>
The static GetLoginStatus method determines if the user is logged in using Request.IsAuthenticated method . If the user is logged in, then the user name is grabbed from the page context's identity object and a hyperlink to a LogOut page is created, along with a hyperlink to a member-only Web page. If the user is not logged in, a hyperlink to a Login page is displayed with the text "Login/Register."
[VB]
Public Shared Function GetLoginStatus(ByVal context As HttpContext) As String
If context.Request.IsAuthenticated Then
Return "Logged In as " & context.User.Identity.Name & _
"<br/><a href='memberpages/members.aspx'>Member Page</a>" & _
"<br/><a href='logout.aspx'>Log Out</a>"
Else
Return "<a href=login.aspx'>Login/Register</a>"
End If
End Function
[C#]
static string GetLoginStatus2(HttpContext context)
{
if (context.Request.IsAuthenticated)
{
return "Logged In as " + context.User.Identity.Name +
"<br/><a href='members.aspx'>Member Page</a>" +
"<br/><a href='logout.aspx'>Log Out</a>";
}
else
{
return "<a href='login.aspx'>Login/Register</a>";
}
}
More Efficient, More Flexible
Using the different tools of post-cache substitution adds another layer of flexibility to the Web sites we build. We no longer have to choose between the efficiency of caching and the flexibility of dynamic content.
For more information on using Post-Cache substitution, take a look at the MSDN document, Dynamically Updating Portions of a Cached Page. You can also see post-cache substation at work in the AdRotator control of ASP.NET.
![]() |
|


