跨多个页面的MVC3 RenderPartial缓存

And*_*gne 7 asp.net-mvc renderpartial razor asp.net-mvc-3

任何人都可以告诉我是否可以跨多个页面缓存RenderPartial?我有一个用户配置文件的RenderPartial,除非用户更新其配置文件,否则不应该真正改变.因此,每次加载页面时,我都不想回去获取他/她的个人资料.我宁愿传递部分内容直到我被迫更新(即个人资料更新)

我查看了p.haack放在一起的DonutHole示例,但它似乎与单个页面相关.有人能指出我正确的方向或提供任何建议吗?或者我一次只能缓存一页?谢谢!

Dar*_*rov 11

您可以改用RenderAction.例:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

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

    [OutputCache(Duration = 6000, VaryByParam = "none")]
    public ActionResult Cached()
    {
        // You could return whatever you want here, even a view
        // but for the purpose of the demonstration I am simply
        // returning a dynamic string value
        return Content(DateTime.Now.ToLongTimeString(), "text/html");
    }
}
Run Code Online (Sandbox Code Playgroud)

Index.cshtmlAbout.cshtml视图中你可以包含子动作:

<div>
    @{Html.RenderAction("Cached");}
</div>
Run Code Online (Sandbox Code Playgroud)

并且你将在两个页面中获得它的缓存版本.