Sitecore - 使用渲染参考动态渲染视图/控制器渲染

时间:2016-06-20 18:24:26

标签: c# asp.net-mvc sitecore

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

我可以渲染类型" Controller渲染"的项目,但不能渲染"查看渲染"。

@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)

2 个答案:

答案 0 :(得分:0)

最近,在我的情况下,我遇到了一个相同的问题,我创建了一条自定义MVC路线,并且需要渲染Item的所有渲染图。

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

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

此解决方案对我有用。

也许可以帮助某人并节省一些时间。

答案 1 :(得分:-2)

您是否尝试过使用@Html.Sitecore().ViewRendering("path to rendering")