Parag Meshram
Parag Meshram

Reputation: 8521

UserControls - Programmatically setting output caching duration?

I want duration to be configurable from Web.config file, so user can alter the output caching after deployment.

For achieving such purpose I need equivalent C# codebehind snippet of following ASP.NET markup?

<%@ OutputCache Duration="120" VaryByParam="CategoryName" %>

Upvotes: 2

Views: 1550

Answers (2)

Dan Diplo
Dan Diplo

Reputation: 25349

There is a way of programmatically setting cache duration for pages, though I'm not sure if this works for partial caching of user controls:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.VaryByParams["Category"] = true;
Response.Cache.SetNoServerCaching();

See http://support.microsoft.com/kb/323290

You can also add a PartialCaching() attribute to a user control to define caching:

[PartialCaching(120)]
public partial class CachedControl : System.Web.UI.UserControl
{
    // Class Code
}

Though I'm not sure how this could be manipulated programmatically, but it might give you some ideas.

Upvotes: 5

Jan Jongboom
Jan Jongboom

Reputation: 27342

You can set output caching using the Response.Cache property.

In this case:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(120));
Response.Cache.VaryByParams["Category"] = true;

http://msdn.microsoft.com/en-us/library/y18he7cw%28VS.71%29.aspx

Upvotes: 0

Related Questions