Tuesday, April 22, 2008

Cacheable ASP.NET pages

Or "Caching ASPX pages"
Or "How to make ASP.NET pages be stored in the browser's cache?"

I used to save every static page in my Web Projects as .HTML files. But Sandro, from MS-Developers taught me how to make cacheable ASPX pages:

Just insert the following C# .NET code into the Page_Load
Response.Cache.SetExpires(DateTime.Now.AddYears(3));
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
You can use the if(!IsPostBack) condition enclosing the code above, for AJAX Pages.

Researching a little more I found an alternative method, just include in the beggining of the ASPX file:
<%@ OutputCache Duration="31536000" Location="Any" VaryByParam="none" %>
As written at ASP Alliance ASP.NET reference

But if you want to disable cacheability use the following code:
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

No comments: