为了账号安全,请及时绑定邮箱和手机立即绑定

如何以编程方式清除控制器动作方法的outputcache

如何以编程方式清除控制器动作方法的outputcache

元芳怎么了 2019-10-17 14:28:47
如果控制器操作具有在操作上指定的OutputCache属性,是否有任何方法可以清除输出缓存而不必重新启动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 );}我正在寻找HttpResponse.RemoveOutputCacheItem(string path)用于清除它的方法,但是在弄清楚将其映射到操作方法的路径时遇到了麻烦。我将再次尝试使用由ViewName呈现的aspx页面。也许我只是手动插入的输出RenderToString入HttpContext.Cache,而是如果我不明白这一个。更新资料请注意,OutputCache是VaryByParam,测试硬编码路径“ / controller / action”实际上并不会清除outputcache,因此看起来它必须匹配“ / controller / action / param1 / param2”。这意味着我可能不得不恢复到对象级缓存,并手动缓存RenderToString():(
查看完整描述

3 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

尝试这个


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 System.Web.Mvc.UrlHelper(requestContext);

更新:


尝试这个:


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

否则,缓存删除将无法进行,因为您已将HTML输出缓存在用户计算机上


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 636 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号