开发者

ASP.NET MVC: Clear an action's cache from another action

Is it possible to clear one action's cache from another action?

Let's say my Index action lists all my Widgets. There are lots of Widgets but new ones are not created very often. So I want to cache my Index action indefinitely but force it to render after a successful Create.

public class WidgetController : Controller
{
    [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
    public ActionResult Index()
    {
        return View(Widget.AllWidgets);
    }

    public ActionResult Create()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(string name)
    {
        Widget widget = new Widget(name);

        // 开发者_JS百科Can I clear Index's cache at this point?
        // ClearCache("Index");

        return View(widget);
    }
}


HttpResponse.RemoveOutputCacheItem?


Use a VaryByCustom property to expire the cache whenever a new Widget is added.


IMHO if you call the Create action you won't hit the cache because you are just rendering a view and not redirecting to the Index action whose output has been cached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜