We add a page to the website built in vs. net2005, drag a subsitution from the toolbox to the page, and write: getcurrenttime in the methodname on its property panel.
In the source file header of this ASPX page, add:
<% @ Outputcache duration = "30" varybyparam = "NONE" %>
This means that this page uses the output cache, And the cache duration is 30 seconds.
Then we can write this method in the background file:
Public static string getcurrenttime (httpcontext context)
{
Return datetime. Now. tostring ();
}
Note that the static keyword must be used here, because the subsitution Cache control only supports static methods, and the parameter can only be httpcontext. Note that no errors will occur.
Okay, now we run the page, and the page will display the time displayed. we refresh the page and the display time has changed. That is to say
No cache is used here. Of course, if you do not believe it, you can also drag a label into the page, and then in the background page_load
Write in method:
Label1.text = datetime. Now. tostring ();
Then run again. At the beginning, the two times displayed on the page are the same, both of which are the current time. However, after refreshing the page again, we can see that, the time on the subsitution Cache control is changed to the current time, while the time on label1 is not changed or the time when we opened the page, that is,Code: <% @ Outputcache duration = "30" varybyparam = "NONE" %> the cache is used on label1. The time above label1 will not change within 30 seconds.