ASP.NET“甜甜圈缓存”不工作
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要缓存页面的多个版本。您需要一个用于登录视图,另一个用于访客视图。您可以通过 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
根据您使用会话的评论,请知道这是替换控制的常见问题:会话在设计上在回调方法中不可用。 (它列在 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...