【问题标题】:Can I use VaryByCustom with a Sitecore 7 Controller Rendering?我可以将 VaryByCustom 与 Sitecore 7 控制器渲染一起使用吗?
【发布时间】:2014-01-22 15:13:05
【问题描述】:

我有一个 Sitecore 7 控制器渲染。我需要通过自定义方法改变 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 方法触发,并且控制器的行为就好像它根本没有 OutputCache 属性...就好像它实际上只是在执行默认的“Cachable”、“VaryByData” ",来自 Sitecore 的 "VaryByParm" 行为。

有什么线索吗?

【问题讨论】:

    标签: c# sitecore outputcache sitecore7


    【解决方案1】:

    好的,我就是这样做的。

    我在/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"];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多