gpt4 book ai didi

c# - 如何更改管道中的 RenderingContext?

转载 作者:行者123 更新时间:2023-11-30 16:50:43 24 4
gpt4 key购买 nike

我需要一个管道来拦截站点核心的构建 RenderingContext ,特别是我需要改变 RenderingContext.Current.Rendering.DataSource动态属性。

我需要这样做,因为我在 sitecore 的数据源中添加了一个变量。我在 Controller 中对此进行了操作,但是当我打开体验编辑器时,它甚至在碰到我的 Controller 之前就倒下了。我猜更高层需要数据源有效。

最佳答案

经过一番挖掘后,我发现了这条管道:

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

反射(reflect)出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);
}
}

我在特定场景中操作数据源,但这段代码可以适应做很多工作。

你这样注册:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.renderRendering>
<processor type="Namespace.MyEnterRenderingContext, DLLName"
patch:instead="*[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.EnterRenderingContext, Sitecore.Mvc']"/>
</mvc.renderRendering>
</pipelines>
</sitecore>
</configuration>

一个问题是它出现在 BrokenLinkValidator 中。你可以覆盖它并创建你自己的:

[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;
}
}

关于c# - 如何更改管道中的 RenderingContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34631661/

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