The @ OutputCache directive declaratively controls the output caching policies of a page.
<%@ OutputCache Duration="cachetime" Vary="headers" %>
Attributes
Example
This example demonstrates how responses from the output cache can be varied to respond to the appropriate language.
<%@ page language="c#"%> <%@ import namespace="System"%> <%@ import namespace="System.Globalization"%> <%@ import namespace="System.Threading"%> <% Random ran; long ticks; DateTime date, now; String lang, accept; ran = new Random(DateTime.Now.Second); ticks = ran.Next(0, 65536) | ((long)ran.Next(0, 65536) << 16) | ((long)ran.Next(0, 65536) << 32) | ((long)ran.Next(0, (int)(DateTime.MaxValue.Ticks >> 48)) << 48); date = new DateTime(ticks); now = DateTime.Now; Response.Cache.SetExpires(DateTime.Now.AddSeconds(10)); Response.Cache.SetLastModified(now); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.Vary.UserLanguage = true; accept = Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]; if (accept.IndexOf("de") > - 1) lang = "de"; else if (accept.IndexOf("fr") > - 1) lang = "fr"; else if (accept.IndexOf("es") > - 1) lang = "es"; else if (accept.IndexOf("ja") > - 1) lang = "ja"; else if (accept.IndexOf("zh") > - 1) lang = "zh"; else lang = "en"; Thread.CurrentThread.CurrentCulture = new CultureInfo(lang); Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang); %> <html> <head> <title>Output Cache Sample</title> </head> <body> <h2>Output Cache Sample</h2> <p>This sample demonstrates use of the output cache to vary by language. The output cache is enabled whenever <b>HttpCachePolicy.SetExpires</b> is set to a date in the future, and ii) <b>HttpCachePolicy.SetCacheability</b> is set to <b>HttpCacheability.Public</b>. Finally, to vary by language, set <b>HttpCachePolicy.Vary.UserLanguage</b> to true.</p> <hr> <p>The request Accept-Language: header appears below:<br> <% Response.Output.WriteLine("<pre>Accept-Language: {0}</pre>", accept); %> <hr> <p>A random date is created, and localized below:<br> <% Response.Output.WriteLine("<pre>{0:F}</pre>", date); %> <hr> <p>The value of the response Last-Modified: header appears below:<br> <% Response.Output.WriteLine("<pre>Last-Modified: {0:r}</pre>", now); %> <hr> </body> </html>
See also
ASP+ Page Syntax | Directives