Home > Interview Questions > ASP.NET > What is outputcache directive in asp.net?

What is outputcache directive in asp.net?

by Thamil   on 04/10/2015   Category: ASP.NET   |  Level: Advance   |  Views: 1037    |  Points: 25     |  Gold 

The outputcache directive is used in asp.net to control the output caching policies of specific asp.net web pages or user control contained in a web page.
Output caching allows user to cache the page output for some time and will serve for future request from cache and its performance benefit to asp.net pages. Below is the syntax for the @outputcache directive.
<%@ OutputCache Duration="#ofseconds"
Location="Any | Client | Downstream | Server | None |
ServerAndClient "
Shared="True | False"
VaryByCustom="browser | customstring"
CacheProfile="cache profile name | ''"
NoStore="true | false"

Sample code:

I have configured the page cache duration as 10second and when you run the application while page load date and time would be displayed and it would not refresh the datetime even if you click the button, after 10 seconds if you click on the button then datetime will be refreshed because we cached the page output for 10saeconds so same we can able to cache with specific controls as well.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OutputCache.aspx.cs" Inherits="IQsamples.OutputCache" %>
<%@ OutputCache Duration="10" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OutputCache sample</title>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" />


protected void Page_Load(object sender, EventArgs e)
Label1.Text = DateTime.Now.ToString();

« What is the use of viewstate in asp.net?
» What is Global.asax file in asp.net web application?
Post Question  |  Question Home

Recent Posts

User Responses

No response found, be the first to review this question.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts