3

I have an ASP.NET page where I am trying to do some output caching, but ran into a problem. My ASPX page has

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MYProject._Default" %>
<%@ OutputCache Duration="600" VaryByParam="None" %>
<%@ Register TagPrefix="MYProjectUC" TagName="PageHeader" Src="~/Lib/UserControls/PageHeader.ascx" %>
<%@ Register TagPrefix="MYProjectUC" TagName="PageFooter" Src="~/Lib/UserControls/PageFooter.ascx" %>

I have a user control called "PageHeader" in the ASPX page. In PageHeader.ascx, I have an ASP.NET Substitution control, where I want to show some links based on the logged in user.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageHeader.ascx.cs" Inherits="MyProject.Lib.UserControls.PageHeader1" %>
<div class="headerRow">
    <div class="headerLogo">
        <a href="Default.aspx"><img src="Lib/Images/header.gif" alt=""></a>
    </div>
    <div id="divHeaderMenu" runat="server">         
        <asp:Substitution ID="subLinks" runat="server" MethodName="GetUserProfileHeaderLinks" />
    </div>   
</div><!--headerRow-->

In my user control code-behind I have a static method which will return a string based on whether the used logged in or not using session:

public static string GetUserProfileHeaderLinks(HttpContext context)
{
    string strHeaderLinks = string.Empty;
    // check session and return string
    return strHeaderLinks;
}

But the page still shows the same content for both logged in user and Guest user.

My objective is to to have the page be cached except the content inside the substitution control. How do I do this?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • If you add a breakpoint inside GetUserProfileHeaderLinks, is hit ALWAYS? – Claudio Redi Apr 08 '10 at 16:48
  • Does it work fine if you remove the OutputCache? – Glennular Apr 08 '10 at 17:05
  • Yes its being hit each time.But Even if i have some value in session,its showing my sesion is null. I removed the Output cache directive from the Default.aspx and It worked well.But does caching workin here ? No i think.right ? – Shyju Apr 09 '10 at 15:51

2 Answers2

1

You are going to want to cache multiple versions of your page. You will want one for the Logged in view and one for the guest view. You can set the two different views either by VaryByParams or VaryByHeaders.

http://msdn.microsoft.com/en-us/library/aa719665%28v=VS.71%29.aspx

Glennular
  • 17,827
  • 9
  • 58
  • 77
0

Based on your comments that you use the Session, know that this is a common issue with Substitution control: The Session is not available in the callback method by design. (It is listed in Context instance, but is always null.)

See the Answer to How to use ASP.Net server controls inside of Substitution control? for a way to do this — though I have double-checked to see if the render control hack initializes the Session or not...

Community
  • 1
  • 1
Jon Adams
  • 24,464
  • 18
  • 82
  • 120