views:

809

answers:

3

If the controller action has the OutputCache attribute specified on an action, is there anyway to clear the outputcache without having to restart IIS?

[OutputCache (Duration=3600,VaryByParam="param1;param2")]
public string AjaxHtmlOutputMethod(string param1, string param2)
{
  var someModel = SomeModel.Find( param1, param2 );

  //set up ViewData
  ...

  return RenderToString( "ViewName", someModel );
}

I'm looking at using HttpResponse.RemoveOutputCacheItem(string path) to clear it, but I'm having trouble figuring out what the path should be to map it to the action method. I'm going to try again with the aspx page that is rendered by ViewName.

Possibly I'll just manually insert the output of RenderToString into the HttpContext.Cache instead if I can't figure this one out.

Update

Please note that the OutputCache is VaryByParam, and testing out a hardcoded path "/controller/action" does not actually clear the outputcache, so it looks like it has to match "/controller/action/param1/param2".

That means I'll probably have to revert to object level caching and manually cache the output for RenderToString() :(

+4  A: 

Try this:

var urlToRemove = Url.Action("AjaxHtmlOutputMethod", "Controller");
Response.RemoveOutputCacheItem(urlToRemove);

http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-caching/230/RemoveOutputCacheItem-question

UPDATED:

var requestContext = new System.Web.Routing.RequestContext(
    new HttpContextWrapper(System.Web.HttpContext.Current),
    new System.Web.Routing.RouteData());

var Url = new UrlHelper(requestContext);

UPDATED:

Try this:

[OutputCache(Location="Server", Duration=3600,VaryByParam="param1;param2")]

Otherwise the cache deletion won't work because you've cached the HTML output on the user's machine

eu-ge-ne
OK, I'll give this a try, thanks!
marcel_g
Of course, now I'm having trouble figuring out how to instantiate a UrlHelper inside the webservice that has the function. Arg.
marcel_g
I gave up on that attempt. I couldn't find any way to get the RequestContext to pass into the UrlHelper constructor method. So I tested it out by hard coding in "/controller/action". This shows no error, but does not clear the cache.
marcel_g
I've updated my answer
eu-ge-ne
Thanks, didn't know about the HttpContextWrapper(), so that actually builds and runs. Unfortunately, it still doesn't clear the outputcache due to the varybyparam that's been set on the method attribute, so I've ended up caching the output of RenderToString using a key that includes the params to differentiate it.
marcel_g
I'll mark it as the answer anyway, because it's probably the closest possible answer anyway.
marcel_g
Question: So, if I want to use this to clear cache for the ENTIRE application, I would need to loop through all the possible conterller/action combinations and call Response.RemoveOutputCacheItem()??If so, is there an easy way to do this?
Kevin
I think I answered my own question:http://stackoverflow.com/questions/11585/clearing-page-cache-in-asp-net/2876701#2876701
Kevin
A: 

Another option is to use VaryByCustom for the OutputCache and handle the invalidation of certain cache elements there.

Maybe it works for you, but it's not a general solution to your problem

chris166
A: 

I think correct flow is:

filterContext.HttpContext.Response.Cache.SetNoStore()

Egor