至于其他的答案已经指出,甜甜圈缓存“之类的”在MVC作品缓存。
我不会推荐它 - 相反,我会提供一个alterantive:
你必须为用户带来查看资料,让我们把它称为“UserProfile.aspx”。
现在在这个视图中,你有一堆HTML,包括“最近的帖子”部分。
现在,我假设这是类似于最后10个帖子为用户。
我会做的就是把这个HTML /段划分为局部视图,并通过一个单独的操作方法为它服务,也叫做PartialViewResult:
public class UserProfileController
{
[HttpGet]
[OutputCache (Duration=60)]
public ActionResult Index() // core user details
{
var userProfileModel = somewhere.GetSomething();
return View(userProfileModel);
}
[HttpGet]
public PartialViewResult DisplayRecentPosts(User user)
{
var recentPosts = somewhere.GetRecentPosts(user);
return PartialViewResult(recentPosts);
}
}
使用呈现出局部视图的jQuery:
<script type="text/javascript">
$(function() {
$.get(
"/User/DisplayRecentPosts",
user, // get from the Model binding
function (data) { $("#target").html(data) } // target div for partial
);
});
</script>
这样,你可以最大化OutputCache的核心细节(Index()),但最近的帖子没有被缓存。 (或者你可以有一个非常小的缓存期)。
呈现部分的jQuery方法与RenderPartial不同,因为这样您可以直接从控制器提供HTML,因此您可以相应地控制输出缓存。
最终结果与甜甜圈缓存非常相似(缓存部分页面,其他部分不缓存)。
另请参阅http://stackoverflow.com/questions/4082826/when-and-how-to-go-about-performing-caching-in-asp-net-mvc/4091232#4091232 – 2010-11-06 03:27:42
任何人都知道答案?我的更新? – Rana 2010-11-08 21:09:46
您需要使用RenderPartial的第四个重载(http://msdn.microsoft.com/zh-cn/library/dd470561.aspx)尝试:'Html.RenderPartial(“UserPosts.ascx”,Model.UserPosts,new ViewDataDictionary {Model = Model.UserPosts}'。 – RPM1984 2010-11-09 04:18:59