简体   繁体   中英

Partial Page Caching and VaryByParam in 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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM