HTT presponse.RemoveOutputCacheItem不工作 [英] HttpResponse.RemoveOutputCacheItem is not working

查看:544
本文介绍了HTT presponse.RemoveOutputCacheItem不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被缓存一个ActionResult。

I have an ActionResult which is cached.

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

The good part is that the cache is working. After the first load, the result is shown without any 4seconds delay.

不过,我需要清除缓存时,一些变化已经到该产品的。

However, i need to clear the cache when some changes has been made to that product.

我试图清除缓存做这样的:

I tried to clear cache doing like this:

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

我也试过打电话给RemoveCache方法既Ajax和页面刷新,其中非正常工作。

I also tried to call RemoveCache method with both ajax and full page refresh, and non of them is working.

我能做些什么?问题出在哪里?

What can i do? Where is the problem?

推荐答案

RemoveOutputCacheItem 与路由参数才能正常运行,没有查询字符串。所以,你可以修改你的路由定义:

The RemoveOutputCacheItem works only with route parameters, not query string. So you could modify your route definition:

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

现在您可以使用RemoveOutputCacheItem方式:

Now you can use the RemoveOutputCacheItem method:

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

查看(〜/查看/主页/ Index.cshtml

@{
    var productId = Guid.NewGuid();    
}

@Html.ActionLink("product 1", "ProductPreview", new { product_id = Guid.NewGuid() })
<br/>
@Html.ActionLink("product 2", "ProductPreview", new { product_id = productId })
<br/>
@Html.ActionLink("product 3", "ProductPreview", new { product_id = Guid.NewGuid() })
<br />

@Html.ActionLink(
    "clear cache for the second product", 
    "RemoveCache", 
    new { product_id = productId }
)

局部视图(〜/查看/主页/ _Foo.cshtml

@model string
@Model

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


UPDATE 2:

现在,你已经证明你的code似乎你正在使用的 Html.RenderAction 帮助和产品preVIEW 是一个孩子的行动。儿童行为不存储在相同的缓存正常的意见和的Htt presponse.RemoveOutputCacheItem 助手不缓存孩子的行为在所有的工作。如果你在我的previous例如仔细看,你会看到我用了产品preVIEW 操作标准链接。

Now that you have shown your code it seems that you are using the Html.RenderAction helper and the ProductPreview is a child action. Child actions are not stored in the same cache as normal views and the HttpResponse.RemoveOutputCacheItem helper doesn't work at all with cached child actions. If you look carefully in my previous example you will see that I used standard links for the ProductPreview action.

目前你正在尝试实现是不可能在ASP.NET MVC 3,如果你想用甜甜圈输出缓存我会建议你的下面的文章。希望这个功能将在ASP.NET MVC 4加入。

Currently what you are trying to achieve is not possible in ASP.NET MVC 3. If you want to use donut output caching I would recommend you the following article. Hopefully this functionality will be added in ASP.NET MVC 4.

这篇关于HTT presponse.RemoveOutputCacheItem不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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