Asp.net mvc HttpResponse.RemoveOutputCacheItem不工作

Asp.net mvc HttpResponse.RemoveOutputCacheItem不工作,asp.net-mvc,model-view-controller,caching,Asp.net Mvc,Model View Controller,Caching,我有一个缓存的ActionResult [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"

我有一个缓存的ActionResult

[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秒延迟

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

我尝试通过以下方式清除缓存:

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仅适用于路由参数,而不适用于查询字符串。因此,您可以修改路线定义:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{product_Id}",
    new { controller = "Home", action = "Index" }
);
现在可以使用RemoveOutputCacheItem方法:

public ActionResult RemoveCache(Guid product_Id)
{
    var url = Url.Action("ProductPreview", "Common", 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");
}

更新:

下面是我的测试用例:

控制器:

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
):

global.asax
中:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{product_id}",
        new { controller = "Home", action = "Index", product_id = UrlParameter.Optional }
    );
}

更新2:

现在,您已经显示了代码,似乎正在使用
Html.RenderAction
帮助程序,
ProductPreview
是一个子操作。子操作与普通视图不存储在同一个缓存中,
HttpResponse.RemoveOutputCacheItem
helper对缓存的子操作根本不起作用。如果您仔细查看我前面的示例,您会发现我使用了
ProductPreview
操作的标准链接


目前,在ASP.NET MVC 3中,您试图实现的目标是不可能实现的。如果您想使用甜甜圈输出缓存,我建议您使用。希望此功能将添加到ASP.NET MVC 4中。

我尝试在Global.asax文件中设置路由,如“{controller}/{action}/{product_Id}”,但仍然不起作用。我还尝试使用VaryByCustom=“product\u Id”,但也不起作用。这与巴黎人的观点有关吗?@RaraituL,不,我不认为这与这是一个片面的观点这一事实有关。我已经更新了我的答案,以提供我的完整测试用例。我用相同的场景创建了一个测试项目,它不起作用,我不明白为什么。我甚至从ActionResult中删除了参数。是web.config设置吗?我需要将缓存存储在服务器上,还是更改一些默认设置?我会发一些images@RaraituL啊,啊,既然你已经展示了你的代码,那么它为什么不工作就很清楚了。您正在使用Html.RenderAction,这意味着ProductPreview是一个子操作。HttpResponse.RemoveOutputCacheItem方法对于子操作根本不起作用。我会更新我的帖子。最后,我也在互联网上搜索,发现了“ChildActions”缓存问题。现在我将玩一点甜甜圈。
@model string
@Model
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{product_id}",
        new { controller = "Home", action = "Index", product_id = UrlParameter.Optional }
    );
}