asp.net-mvc – HttpResponse.RemoveOutputCacheItem不工作

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – HttpResponse.RemoveOutputCacheItem不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个ActionResult缓存. @H_502_2@[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)); }

很高兴的是缓存正在工作.第一次加载后,结果显示没有任何4秒的延迟.

但是,当对该产品进行某些更改时,我需要清除缓存.

我试图清除缓存做如下:

@H_502_2@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"); }

我还尝试使用ajax和全页刷新调用RemoveCache方法,而不是它们正在工作.

我能做什么?哪里有问题?

解决方法

RemoveOutputCacheItem仅适用于路由参数,而不是查询字符串.所以你可以修改你的路线定义: @H_502_2@routes.MapRoute( "Default","{controller}/{action}/{product_Id}",new { controller = "Home",action = "Index" } );

现在可以使用RemoveOutputCacheItem方法

@H_502_2@public ActionResult RemoveCache(Guid product_Id) { var url = Url.Action("ProductPreview",new { product_Id = product_Id }); // the url must look like this: /Common/ProductPreview/eeb2fe32-db58-4fc3-87c8-b47480fbe094 // for the RemoveOutputCacheItem method to work HttpResponse.RemoveOutputCacheItem(url); return RedirectToAction("Index"); }

更新:

这是我的测试用例:

控制器:

@H_502_2@public class HomeController : Controller { public ActionResult Index() { return View(); } [OutputCache(Duration = 3600,VaryByParam = "product_id")] public ActionResult ProductPreview(Guid product_id) { var model = string.Format( "{0} - {1}",product_id,DateTime.Now.ToLongTimeString() ) return PartialView("_Foo",model); } public ActionResult RemoveCache(Guid product_id) { var url = Url.Action( "ProductPreview","Home",new { product_id = product_id } ); HttpResponse.RemoveOutputCacheItem(url); return RedirectToAction("Index"); } }

查看(〜/ Views / Home / Index.cshtml):

@H_502_2@@{ var productId = Guid.NewGuid(); } @Html.ActionLink("product 1","ProductPreview",new { product_id = Guid.NewGuid() }) <br/> @Html.ActionLink("product 2",new { product_id = productId }) <br/> @Html.ActionLink("product 3",new { product_id = Guid.NewGuid() }) <br /> @Html.ActionLink( "clear cache for the second product","RemoveCache",new { product_id = productId } )

部分视图(〜/ Views / Home / _Foo.cshtml):

@H_502_2@@model string @Model

并在全球.asax:

@H_502_2@public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default","{controller}/{action}/{product_id}",action = "Index",product_id = UrlParameter.Optional } ); }

更新2:

现在你已经显示了你的代码,好像你正在使用Html.RenderAction帮助器,而ProductPreview是一个子操作.子动作与普通视图不存储在相同的缓存中,HttpResponse.RemoveOutputCacheItem帮助器根本不起作用,缓存子操作.如果您仔细阅读我之前的示例,您将看到我使用标准链接进行ProductPreview操作.

目前您正在尝试实现的是ASP.NET MVC 3中无法实现的.如果要使用甜甜圈输出缓存,我建议您使用following article.希望此功能添加到ASP.NET MVC 4中.

猜你在找的asp.Net相关文章