输出缓存、页面加载和回发 - 似乎有两个版本的页面缓存
我正在学习 ASP.NET 输出缓存。
我整理了一个非常简单的页面(见下文)。测试没有用;这只是为了说明这个问题所涉及的行为。
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Guid.NewGuid().ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
当页面第一次加载时,文本框为空(如预期)。
第一次单击该按钮时,文本框会填充 SOMEGUID。根据我的阅读,我预计它会保持为空...因为该页面应该是从缓存中提供的...?
对于后续的按钮单击,文本框内容将保持为 SOMEGUID(直到缓存过期,在这种情况下它是 SOMEOTHERGUID)。
如果我将页面加载到另一个浏览器选项卡中(通过复制并粘贴 URL),则文本框为空。
如果我单击新页面中的按钮,文本框内容将更改为 SOMEGUID 并保持这种状态(直到缓存过期,在这种情况下它是 SOMEOTHERGUID)。
因此,缓存中似乎有两个版本;一个用于新加载的页面,第二个用于第一个按钮单击的结果?怎么了?我可以阻止它(出于实验目的)吗?我尝试将 VarByControl 属性设置为“none”,但没有任何效果...
这与其他一些问题类似,几乎与 输出缓存和回发。然而,在我发现的类似问题中,没有一个包含完整的代码示例来说明行为,也没有一个接受答案。
更新:我还没有解决这个问题,也没有找到一篇讨论它的文章。我打开了页面的追踪功能,看看是否有任何帮助。但是,通过跟踪,每次回发都会生成一个新的 GUID。仍在谷歌搜索...
I am learning about ASP.NET output caching.
I put together a very simple page (see below). The test isn't useful; it's just to illustrate the behaviour this question is about.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Guid.NewGuid().ToString();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
When the page loads for the first time, the textbox is empty (as expected).
The first time the button is clicked, the textbox populates with SOMEGUID. From my reading I expected it to remain empty...since the page is supposed to have been served from the cache...?
For subsequent button clicks, the textbox content remains as SOMEGUID (until the cache expires in which case it is SOMEOTHERGUID).
If I load the page into another browser tab (by copying and pasting the URL) the texbox is empty.
If I click the button in the new page, the textbox content changes to SOMEGUID and stays that way (until the cache expires in which case it is SOMEOTHERGUID).
So, there appears to be two versions in the cache; one for the freshly loaded page and a second for the result of the first button click? What is happening? Can I prevent it (for experimental purposes)? I tried setting the varyByControl attribute to "none" but it had no effect...
This is similar to a few other questions and almost an exact duplicate of output Caching and postback. However, of the similar questions that I have found, none include a full code sample to illustrate the behaviour and none have accepted answers.
UPDATE: I have still not worked it out or found an article that discusses it. I turned tracing on for the page to see if that shed any light. However, with tracing on a new GUID is generated for every postback. Still googling...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看了一下输出缓存实现(OutputCacheModule),缓存键是唯一的,基于以下几点:
在您的情况下,GET 和 POST 最终会创建两个不同的缓存键
此问题中发布了此设计限制的解决方法。
I took a peek inside the output caching implementation (the OutputCacheModule) and the cache key is unique based on a couple of things, namely:
In your case the GET and the POST end up creating two different cache keys.
A workaround for this design limitation is posted in this question.