如何为控制器的操作方法编程明确的OutputCache [英] How to programmatically clear outputcache for controller action method

查看:103
本文介绍了如何为控制器的操作方法编程明确的OutputCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果控制器动作,对动作指定的OutputCache属性,有没有什么办法来清除输出缓存,而无需重新启动IIS?

If the controller action has the OutputCache attribute specified on an action, is there any way to clear the output cache 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 );
}

我期待在使用的Htt presponse.RemoveOutputCacheItem(字符串路径)来清除它,但我有麻烦搞清楚的路径应该是什么它映射到操作方法。我将与由VIEWNAME呈现aspx页面重试。

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.

也许我只是手动插入 RenderToString 输出到 HttpContext.Cache ,而是如果我能' W图这一个。

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

更新

请注意,的OutputCache的VaryByParam时,并测试了硬codeD路径/控制器/动作实际上并没有明确的OutputCache,所以它看起来像它必须匹配/控制器/动作/参数1 /参数2。

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".

这意味着我可能会恢复到对象级别的缓存和缓存手动输出为 RenderToString():(

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

推荐答案

试试这个

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

更新:

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

var Url = new UrlHelper(requestContext);

更新:

试试这个:

[OutputCache(Location= System.Web.UI.OutputCacheLocation.Server, Duration=3600,VaryByParam="param1;param2")]

否则缓存删除将无法正常工作,因为你已经
缓存在用户的机器上的HTML输出

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

这篇关于如何为控制器的操作方法编程明确的OutputCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆