How to remove output cache for child action (MVC3)

2019-05-15 04:06发布

问题:

I try this code

public ActionResult RemoveCache(Guid product_Id)  
{  
    var url = Url.Action("ProductPreview", "Common", new { product_Id = product_Id });  
    HttpResponse.RemoveOutputCacheItem(url);  
    return RedirectToAction("Index");  
}  

to remove the output cache for a child action, but it does not work. Please help me, thanks!

回答1:

Use the MVCDonutCaching nuget package at: http://mvcdonutcaching.codeplex.com/

See the section entitled "Usage" at: http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3

for removing items from the cache. There are Html.Action overloads with a param you add to force the donut hole (and thus exclude caching)

@Html.Action("Login", "Account", true)

The true above means 'don't cache this child action - create a donut hole'



回答2:

If it is accepted to clear the output cache for ALL chid actions, here is a way:

OutputCacheAttribute.ChildActionCache = new MemoryCache("NewRandomStringNameToClearTheCache"); 

NewRandomStringNameToClearTheCache should be a random string.

Source References: http://dotnet.dzone.com/articles/programmatically-clearing-0