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