MVC3 RenderPartial caching across multiple pages

我是研究僧i 提交于 2019-12-22 04:47:07

问题


Can anyone tell me if its possible to Cache a RenderPartial across multiple pages? I have a RenderPartial for a user profile that shouldn't really ever change unless the user updates their profile. So i dont really want to go back and get his/her profile every time i load a page. I would much rather pass the partial around until im forced to update (i.e. profile update)

I looked at the DonutHole example that p.haack put together, but it seems to be relevant for a single page. Can someone point me in the right direction or offer any advice? Or am i only able to cache one page at a time? thanks!


回答1:


You could use RenderAction instead. Example:

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

and inside the Index.cshtml and About.cshtml views you could include the child action:

<div>
    @{Html.RenderAction("Cached");}
</div>

and it you will get the cached version of it in both pages.



来源:https://stackoverflow.com/questions/5194532/mvc3-renderpartial-caching-across-multiple-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!