gpt4 book ai didi

c# - 我可以将 VaryByCustom 与 Sitecore 7 Controller 渲染一起使用吗?

转载 作者:太空狗 更新时间:2023-10-29 22:26:34 25 4
gpt4 key购买 nike

我有一个 Sitecore 7 Controller 渲染。我需要通过自定义方法改变 OutputCache。

渲染当前在 Sitecore 中设置为“Cachable”、“VaryByData”和“VaryByParm”。

我已经为我的操作添加了一个输出缓存属性,并设置了一个自定义的可变字符串:

[OutputCache(VaryByCustom = "ThisIsATest", Duration = 60)]
public ActionResult Index()
{
...
}

我的 Global.asax 继承自 Sitecore.Web.Application,我已经重写了 GetVaryByCustomString,如下所示:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "ThisIsATest")
return "some custom key";
return base.GetVaryByCustomString(context, custom);
}

我从来没有看到 GetVaryByCustomString 方法被触发, Controller 的行为就好像它根本没有 OutputCache 属性一样......就好像它实际上只是在执行默认的“Cachable”,“VaryByData” ", 来自 Sitecore 的 "VaryByParm"行为。

有什么线索吗?

最佳答案

好的,这就是我的做法。

我在 /sitecore/templates/System/Layout/Sections/Caching 上添加了一个名为“VaryByMyCustomThing”的复选框字段。

然后我用自定义实现替换了 Sitecore.Mvc.config 中的“GenerateCacheKey”管道。我替换了这个:

<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc"/>

有了这个:

<processor type="My.Site.Pipelines.GenerateCustomCacheKey, My.Site"/>

我的 GenerateCustomCacheKey 类如下所示:

using System.Net.Http;
using System.Web;
using Sitecore.Mvc.Extensions;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;

namespace My.Site.Pipelines
{
public class GenerateCustomCacheKey : GenerateCacheKey
{
protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
{
var varyByCountryCode = rendering.RenderingItem.InnerItem["VaryByMyCustomThing"].ToBool();

var key = base.GenerateKey(rendering, args);
if (varyByCountryCode)
key = key + GetCountryCodePart(rendering);
return key;
}

protected string GetCountryCodePart(Rendering rendering)
{
return "_#countryCode:" + (string)HttpContext.Current.Session["CountryCode"];
}
}
}

关于c# - 我可以将 VaryByCustom 与 Sitecore 7 Controller 渲染一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286704/

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