You need to enable output caching in ASP.NET on master pages in your code-behind C# files. Your web site uses a master page and content page system. In this article, then, we see how to utilize output caching on ASP.NET master pages, which will improve server performance.
First, you cannot add an OutputCache directive on a master page. You must do the caching instructions programmatically, in code. Every content page that uses the master page needs output caching enabled.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetValidUntilExpires(true);
}You can test the code by adding a timestamp to every page. Remember, everything that you do not carefully test is probably broken or will break eventually. Here's a way to tell your page to generate a timestamp each time your page is built or generated.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Generated on " + DateTime.Now.ToString();
}Description of the code example. The Page_Load method is always run when a page is not served from the output cache. However, if the page is not from the output cache, this code will update the timestamp.
Page contents. When you browse your page you should get an old timestamp if the page was recently loaded, even when you force a refresh in the browser. The above code assumes you have a Label control with ID of Label1 in your .aspx.
Remember that output cache is a suggestion. Your ASP.NET process can be restarted and the cache can be cleared at any time. Don't worry if the page is regenerated every 15 minutes, even if you put it in the output cache for one month.
(See Expiration Time and Cache.)
What could go wrong? You could cache too much. Never use output caching when you need to do complex interactions with your visitor. There are subtle issues with query strings that I do not go over here. Please see the link above.
In this example, we saw how you can output cache master pages in ASP.NET. With this method, the server's CPU load should be greatly reduced. However, your ASP.NET pages must be fast in the first place if you want a quick site. Finally, output caching sometimes makes debugging your application harder