部分页面缓存和中的VaryByParam ASP.NET MVC 3 [英] Partial Page Caching and VaryByParam in ASP.NET MVC 3

查看:180
本文介绍了部分页面缓存和中的VaryByParam ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ASP.NET MVC 3提供的新的部分页面缓存在我看来,我使用的是:

I'm attempting to use the new partial page caching available in ASP.NET MVC 3. In my view, I'm using:

<% Html.RenderAction("RenderContent", Model); %>

它调用控制器方法:

Which calls the controller method:

[Authorize]
[OutputCache(Duration = 6000, VaryByParam = "*", VaryByCustom = "browser")]
public ActionResult RenderContent(Content content)
{
   return PartialView(content);
}

请注意,无论是原视图和局部视图使用相同的视图模型。

Note that both the original view and the partial view are using the same view model.

问题是,的VaryByParam 不起作用 - RenderContent()总是返回相同的缓存HTML不管什么视图模型传递给它。是否有一些关于的VaryByParam 我不明白?

The problem is that VaryByParam doesn't work - RenderContent() always returns the same cached HTML no matter what view model is passed to it. Is there something about VaryByParam that I don't understand?

推荐答案

我想我想通了。它看起来像问题是VaryByParam时,当输入参数是一个对象,使用的ToString()的对象,以确定它的独特性。因此,这让两个选项:

I think I figured it out. It looks like the issue is that VaryByParam, when the input parameter is an object, uses ToString() on that object to determine it's uniqueness. So this leaves two options:


  1. 重写的ToString()来提供一个唯一的标识符。

  2. 传递的唯一标识符作为附加参数:

  1. Overriding ToString() to provide a unique identifier.
  2. Passing a unique identifier as an additional parameter:

<% Html.RenderAction("RenderContent", Model, Model.Id); %>

[Authorize]
[OutputCache(Duration = 6000, VaryByParam = "id", VaryByCustom = "browser")]
public ActionResult RenderContent(Content content, string id)
{
   return PartialView(content);
}


这篇关于部分页面缓存和中的VaryByParam ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆