开发者

ASP.NET 'Donut Caching' not working

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/PageHe开发者_开发技巧ader.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?


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


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜