I have an ActionResult which is cached.
[OutputCache(Duration = 3600, VaryByParam = "product_Id")]
public ActionResult ProductPreview(Guid product_Id)
{
// just for testing the cache
System.Threading.Thread.Sleep(4000);
return PartialView("ProductPreview", _repository.CreateProductModel(product_Id));
}
The good part is that the cache is working. After the first load, the result is shown without any 4seconds delay.
However, i need to clear the cache when some changes has been made to that product.
I tried to clear cache doing like this:
public ActionResult RemoveCache()
{
var url = Url.Action("ProductPreview", "Common");
// also tried with parameter
// var url = Url.Action("ProductPreview", "Common", new { @product_Id = "productId" });
HttpResponse.RemoveOutputCacheItem(url);
return RedirectToAction("Index");
}
I also tried to call RemoveCache method with both ajax and full page refresh, and non of them is working.
What can i do? Where is the problem?
The
RemoveOutputCacheItem
works only with route parameters, not query string. So you could modify your route definition:Now you can use the RemoveOutputCacheItem method:
UPDATE:
Here's my test case:
Controller:
View (
~/Views/Home/Index.cshtml
):Partial view (
~/Views/Home/_Foo.cshtml
):and in
global.asax
:UPDATE 2:
Now that you have shown your code it seems that you are using the
Html.RenderAction
helper and theProductPreview
is a child action. Child actions are not stored in the same cache as normal views and theHttpResponse.RemoveOutputCacheItem
helper doesn't work at all with cached child actions. If you look carefully in my previous example you will see that I used standard links for theProductPreview
action.Currently what you are trying to achieve is not possible in ASP.NET MVC 3. If you want to use donut output caching I would recommend you the following article. Hopefully this functionality will be added in ASP.NET MVC 4.