MVC 4子操作的outputcache属性不适用于VaryByParam值

时间:2013-08-29 08:46:29

标签: asp.net-mvc-4 outputcache child-actions varybyparam

是否无法根据查询值缓存子操作的输出?

public class HomeController : Controller
{
    public ActionResult About()
    {
        ViewBag.Message = DateTime.Now.ToLongTimeString();

        return View();
    }

    [OutputCache(Duration = 20, VaryByParam = "id")]
    public ActionResult PartialViewTestAbout()
    {
        ViewBag.Second = DateTime.Now.Second;
        return View();
    }
}


About.cshtml

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>


@Html.Action("PartialViewTestAbout")
.
.
.

PartialViewTestAbout.cshtml:
----------
<p>
    This is a partial view About.
    <h1 style="color:red;">@ViewBag.Message</h1>
    @ViewBag.Second
</p>

没有varybyparam,它的工作正常。但我需要刷新PartialViewAboutTest取决于参数。如果我把OutPutCache放在关于ActionREsult与VaryByParam是工作。但是在这个示例中,子动作是PartailViewTestAbout,它不起作用,我改变了查询,但是不等待缓存持续时间很长时间来刷新页面......

1 个答案:

答案 0 :(得分:3)

答案是,缺少的id参数未传递给PartialViewTestAbout ..

[OutputCache(Duration = 20, VaryByParam = "id")]

public ActionResult PartialViewTestAbout(int id)

这很有效。

感谢Cem LEGOZ .. :)