ASP.NET OutputCache and postbacks

lnetanel picture lnetanel · Jul 21, 2009 · Viewed 7.4k times · Source

I'm trying to understand the ASP.NET OutputCache mechanism.
I built a test page with a Label and LinkButton.
The label text is being initialized on the server with the current server date on every PageLoad:

protected void Page_Load(object sender, EventArgs e)  
{  
    lblDate.Text = DateTime.Now.ToString();  
}

I used this directive: <%@ OutputCache Duration="600" VaryByParam="none"%>

When I press the LinkButton the first time I get a new text in the Label but if I press the linkbutton again I don't get a new text.

I assume this is because of the parameters that are being transfered to the server that are the same for each postback.

Is there any way to work with OutputCach and postback controls?

Answer

user134706 picture user134706 · Jul 21, 2009

Right, the thing is you're varying by NO parameter so the first request's response html is cached and served for the next 10 minutes (in theory). If you want to cache GETs but process different POSTs you should vary by your POST params.

Let me give you an example. You have a text input used to send an email with its content on POST. If you vary by that input name, each request within the caching time span with different values for that text input would hit your handler and process sending the email.

On the other side you could vary by * but then you'd lose kernel mode caching.