gpt4 book ai didi

c# - Sitecore - 使用渲染引用动态渲染 View / Controller 渲染

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:14 24 4
gpt4 key购买 nike

我需要在不同的页面上动态呈现一个项目。所以我检索了项目的渲染引用并使用渲染项目 ID 和数据源。

我可以渲染“ Controller 渲染”类型的项目,但不能渲染“ View 渲染”。

@Html.Sitecore().Rendering(renderingItem.RenderingID.Guid.ToString(), new  {DataSource = renderingItem.Settings.DataSource, Cacheable = false, Cache_VaryByData = false});

**Exception Message**: Attempt to retrieve context object of type 'System.Web.Mvc.ViewContext' from empty stack.

**Stack Trace**: at Sitecore.Mvc.Common.ContextService.Peek[T]()
at Sitecore.Mvc.Presentation.ViewRenderer.GetHtmlHelper()
at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Process(RenderRenderingArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
at Sitecore.Mvc.Helpers.SitecoreHelper.RenderRendering(Rendering rendering)
at Castle.Proxies.Invocations.SitecoreHelper_RenderRendering_1.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Jabberwocky.Autofac.Extras.MiniProfiler.Interceptors.AsyncProfilingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.CustomSitecoreHelperProxy.RenderRendering(Rendering rendering)
at Castle.Proxies.Invocations.SitecoreHelper_Rendering_1.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Jabberwocky.Autofac.Extras.MiniProfiler.Interceptors.AsyncProfilingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.CustomSitecoreHelperProxy.Rendering(String pathOrId, Object parameters)

最佳答案

我最近在我的案例中遇到了同样的问题,我创建了一个自定义 MVC 路由,我需要在那里渲染 Item 的所有渲染。

我遇到了同样的问题, Controller 渲染正常但 View 渲染不工作所以我更改了代码来处理同样的问题。我添加了一个条件来检查呈现类型,并在此基础上添加了一些自定义代码来呈现这两种类型。

 if (rendering.RenderingItem.InnerItem["Path"] == null || string.IsNullOrEmpty(rendering.RenderingItem.InnerItem["Path"]))
{
PipelineService.Get().RunPipeline<RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(rendering, writer));
}
else
{
ViewData.Model = rendering.Model;
var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, rendering.RenderingItem.InnerItem["Path"]);
var viewContext = new ViewContext(controllerContext, viewResult.View,
ViewData, TempData, writer);
viewContext.ViewBag.renderingUniqueId = rendering.UniqueId;
viewResult.View.Render(viewContext, writer);
}

这个解决方案适合我。

可能它可以帮助某人并节省一些时间。

关于c# - Sitecore - 使用渲染引用动态渲染 View / Controller 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929299/

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