3

I use the below code for caching

public class HomeController : Controller
{
    [OutputCache(Location=OutputCacheLocation.Server, Duration = 1000, VaryByParam = "id")]
    public ActionResult Index(string id)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //and trying to invalidate cache in here
        Response.RemoveOutputCacheItem(Request.Url.PathAndQuery);
    }
}

I try to invalidate the cache by overriding OnActionExecuting method but that method just called first time. So where do I have to try to call Response.RemoveOutputCacheItem() method to invalidate the cache ?

RickL
  • 3,318
  • 10
  • 38
  • 39
AnyOne
  • 931
  • 2
  • 12
  • 40
  • What are you trying to achieve here? You have a controller with a single action which gets cached, meaning that this controller will be called only once. When do you want to invalidate this cache? Maybe on some other controller action? – Darin Dimitrov Oct 03 '11 at 06:08
  • I want to invalidate this cache when some conditions appear so before execution method i invalidate it. But as you see that OnActionExecution method never called. Now i understand what you try to mean. You think that another action method will decide to invalidate it. It actualy may be or may not to be. But there can be some situations in which i just decide to invalidate it before execute that action. – AnyOne Oct 03 '11 at 13:46
  • the action never executes, so you cannot invalidate the cache before the action executes :-) – Darin Dimitrov Oct 03 '11 at 14:08
  • So is there any another place to invalidate ? Global action filter or IHttpHandler ?? Or any other solution – AnyOne Oct 03 '11 at 14:14
  • 1
    One of the solution appear in my mind is to using Index action indirectly via Redirect by another action method ? What do you think about it ? – AnyOne Oct 03 '11 at 14:17
  • 1
    yes you could have some controller action which evicts the cache and then redirects to Index. – Darin Dimitrov Oct 03 '11 at 14:19

0 Answers0