ASP. NET, code implementation page output Cache

Source: Internet
Author: User

I have been searching online for a long time and found many of them are invalid. Can I only use <% @ outputcache %> to set the page cache? Finally, I found what I wanted in ASP. NET 3.5 decryption.

Add the following in the page_load event on the page:Code:

Protected void page_load (Object sender, eventargs E)
{
Response. cache. setcacheability (httpcacheability. Public );
Response. cache. setexpires (datetime. Now. addseconds (15 ));
// Response. cache. setmaxage (timespan. fromseconds (15); // This sentence is optional. It only determines when the client opens the page and sends a request to the server.
Response. cache. setvaliduntilexpires (true); // This sentence is required. Its function is to ignore the header Pragma: No-cache in the client request information, and the Application Server Page Cache
// Response. cache. setlastmodified (datetime. Now); // if this sentence is not required, the server cannot return the 304 status code.
// Response. cache. setomitvarystar (true); // do not use this sentence
Response. Write (datetime. Now. tostring ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.