- ASP.NET 4.61
- MVC 5
I want to know if it is possible to add output caching to an MVC application at runtime and vary it.
So normally one would declare an output cache attribute as follows:
[OutputCache(Duration = 10]
public ActionResult MyAction()
{
}
What I want to be able to do is to selectively enable and disable output caching on actions at runtime. So if I started out with a deployed application:
public ActionResult One()
{
}
public ActionResult Two()
{
}
and at runtime I decided I wanted Two() to be cached as if I had:
[OutputCache(Duration = 20]
public ActionResult MyAction()
{
}
but then later I want it to be:
[OutputCache(Duration = 50]
public ActionResult MyAction()
{
}
and then later remove caching all together:
public ActionResult MyAction()
{
}
So my question are:
1) Is it possible to control the caching of a controller method at runtime ie. Apply the equivalence of [OutputCache] at runtime? 2) Is there a different (easy) way to vary output caching at runtime?
[OutputCache(Duration = "myDurationProperty"]and use reflection to get the value of the property<outputCacheProfiles><name="xxx" duration="20">etc to you web config. But then you would still need to define a custom attribute that specifies the name of a property that contains the value for the profile name to use (and use reflection to retrieve it)