I\'m trying to use cache profiles for caching child actions in my mvc application, but I get an exception: Duration must be a positive number.
My web.config looks li
Here's a simple way if :
All I did was created a new attribute 'DonutCache'.
[DonutCache]
public ActionResult HomePageBody(string viewName)
{
var model = new FG2HomeModel();
return View(viewName, model);
}
I store my caching setting in Web.config (under a new custom name - so as to avoid confusion).
I created a simple helper method to pull the value out.
public static class Config {
public static int DonutCachingDuration
{
get
{
return int.Parse(ConfigurationManager.AppSettings["DonutCachingDuration"]);
}
}
}
Unfortunately you can only initialize an [Attribute] with a constant, so you need to initialize the attribute in its constructor (you cant just say [Attribute(Config.DonutCachingDuration)] unfortunately).
Note: This doesn't prevent you setting 'varyByParam' in the [DonutCache] declaration - which is currently the only other property that is usable for caching of Action methods.
class DonutCacheAttribute : OutputCacheAttribute
{
public DonutCacheAttribute()
{
// get cache duration from web.config
Duration = Config.DonutCachingDuration;
}
}
Just use an XDT web transformation's and you're ready to deploy with a longer value.
Tip: You'll probably want to stick a @DateTime.Now.ToString() in your partial view to make sure the cache setting is being respected.