ASP.NET“甜甜圈缓存”不工作

发布于 2024-08-28 03:39:20 字数 1484 浏览 12 评论 0原文

我有一个 ASP.NET 页面,我试图在其中进行一些输出缓存,但遇到了问题。 我的 ASPX 页面有

<%@ 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" %>

一个名为“PageHeader”的用户控件。在 PageHeader.ascx 中,我有一个 ASP.NET Substitution 控件,我想在其中根据登录用户显示一些链接。

<%@ 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-->

在我的用户控制代码隐藏中,我有一个静态方法,它将根据用户是否使用会话登录而返回一个字符串:

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

但页面仍然为登录用户和来宾用户显示相同的内容。

我的目标是缓存除替换控件内的内容之外的页面。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-09-04 03:39:20

您将需要缓存页面的多个版本。您需要一个用于登录视图,另一个用于访客视图。您可以通过 VaryByParams 或 VaryByHeaders 设置两个不同的视图。

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

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

疏忽 2024-09-04 03:39:20

根据您使用会话的评论,请知道这是替换控制的常见问题:会话在设计上在回调方法中不可用。 (它列在 Context 实例中,但始终为空。)

请参阅 如何在替换控件内使用 ASP.Net 服务器控件? 的答案,以获取执行此操作的方法 - 尽管我已仔细检查渲染控件 hack 是否初始化会议与否...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文