gpt4 book ai didi

c# - ASP.NET MVC 3 中的部分页面缓存和 VaryByParam

转载 作者:IT王子 更新时间:2023-10-29 04:43:26 25 4
gpt4 key购买 nike

我正在尝试使用 ASP.NET MVC 3 中可用的新部分页面缓存。在我看来,我正在使用:

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

调用 Controller 方法:

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

请注意,原始 View 和分部 View 都使用相同的 View 模型。

问题是 VaryByParam 不起作用 - RenderContent() 总是返回相同的缓存 HTML,无论传递给它的 View 模型是什么。 VaryByParam 有什么我不明白的地方吗?

最佳答案

我想我明白了。看起来问题在于,当输入参数是一个对象时,VaryByParam 在该对象上使用 ToString() 来确定它的唯一性。所以这留下了两个选择:

  1. 覆盖 ToString() 以提供唯一标识符。
  2. 将唯一标识符作为附加参数传递:

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

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

关于c# - ASP.NET MVC 3 中的部分页面缓存和 VaryByParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5144592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com