C# 如何更改管道中的渲染上下文?

C# 如何更改管道中的渲染上下文?,c#,sitecore,sitecore-mvc,C#,Sitecore,Sitecore Mvc,我需要一个管道来拦截sitecore的构建RenderingContext,特别是我需要动态更改RenderingContext.Current.Rendering.DataSource属性 我需要这样做,因为我已经在sitecore的数据源中添加了一个变量。我在控制器中操作它,但当我打开“体验编辑器”时,它甚至在点击“我的控制器”之前就掉了下来。我猜高层需要数据源有效。在对管道进行了一番挖掘之后,我发现了这个管道: namespace Sitecore.Mvc.Pipelines.Respon

我需要一个管道来拦截sitecore的构建
RenderingContext
,特别是我需要动态更改
RenderingContext.Current.Rendering.DataSource
属性


我需要这样做,因为我已经在sitecore的数据源中添加了一个变量。我在控制器中操作它,但当我打开“体验编辑器”时,它甚至在点击“我的控制器”之前就掉了下来。我猜高层需要数据源有效。

在对管道进行了一番挖掘之后,我发现了这个管道:

namespace Sitecore.Mvc.Pipelines.Response.RenderRendering
{
    public class EnterRenderingContext : RenderRenderingProcessor
    {
        public override void Process(RenderRenderingArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (args.Rendered)
            {
                return;
            }
            this.EnterContext(args.Rendering, args);
        }

        protected virtual void EnterContext(Rendering rendering, RenderRenderingArgs args)
        {
            IDisposable item = RenderingContext.EnterContext(rendering);
            args.Disposables.Add(item);
        }
    }
}
反映在
Sitecore.Mvc.dll中


现在,我可以用自己的管道替换此管道,并在构建之前更改
RenderingContext
的值:

public class RedrowEnterRenderingContext : Sitecore.Mvc.Pipelines.Response.RenderRendering.EnterRenderingContext
{
    private const string _developmentKeyword = "$development";

    private IDevelopmentQueryServiceV2 _coUkDevelopmentQueryService = ServiceLocator.Current.GetInstance<IDevelopmentQueryServiceV2>();


    protected override void EnterContext(Rendering rendering, RenderRenderingArgs args)
    {

        //Make your changes to the items that are used to build the context here
        if (args.PageContext != null &&
            args.PageContext.Item != null &&
            args.Rendering.DataSource.Contains(_developmentKeyword) &&
            args.PageContext.Item.TemplateID.Guid == TemplateIdConst.V2Development)
        {

            args.Rendering.DataSource = args.Rendering.DataSource.Replace(_developmentKeyword, 
                args.PageContext.Item.Paths.Path);
        }
        //build the context using the existing functionality
        base.EnterContext(rendering, args);
    }
}

我在这里的博文中也做了类似的事情:它不是MVC tho,但可能有时间使用它会有所帮助。我会看看@Younes。我写这篇文章是因为我努力寻找关于这个主题的任何文档。
[Serializable]
public class MyBrokenLinksValidator : BrokenLinkValidator
{
    public RedrowBrokenLinksValidator() : base()
    {

    }

    public RedrowBrokenLinksValidator(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
    {
    }

    protected override ValidatorResult Evaluate()
    {
        ValidatorResult returnVal = base.Evaluate();
        if (returnVal != ValidatorResult.Valid)
        {
            Item obj = base.GetItem();
            ItemLink[] brokenLinks = obj.Links.GetBrokenLinks(false);
            //are all the broken links basically because they are contextual?
            if (brokenLinks.All(a => a.TargetPath.Contains("$development")))
            {
                foreach (ItemLink brokenLink in brokenLinks)
                {
                    Database database = Sitecore.Configuration.Factory.GetDatabase("master");
                    //try again but replacing the varible with a context
                    var secondTryPath = brokenLink.TargetPath.Replace(
                        "$development", obj.Paths.Path);

                    Item secondTryItem = database.GetItem(secondTryPath);
                    if (secondTryItem == null)
                        return returnVal;
                }

                //if we've got here then all the links are valid when adding the context
                return ValidatorResult.Valid;
            }
        }

        return returnVal;
    }
}