Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET OutputCache and postbacks

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?

like image 437
lnetanel Avatar asked Jul 21 '09 14:07

lnetanel


1 Answers

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.

like image 133
user134706 Avatar answered Oct 19 '22 14:10

user134706