1

We have a web controller inside our Sitecore 8.2 Site that returns a search box view. The controller is exposed as an API endpoint like so: {url}/api/search/large/

When making an http web request to the endpoint it works fine until we try and use: Html.Sitecore().Rendering() within the view to load another view rendering, it causes this error:

  System.InvalidOperationException
      HResult=0x80131509
      Message=Attempt to retrieve context object of type 'System.Web.Mvc.ViewContext' from empty stack.
      Source=Sitecore.Mvc
      StackTrace:
       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 Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
       at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
       at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
       at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
       at Sitecore.Mvc.Helpers.SitecoreHelper.RenderRendering(Rendering rendering)
       at ASP._Page_Views_Shared_Large_OminboxLarge_cshtml.Execute() in c:\Sites\Website\Views\Shared\OminboxLarge.cshtml:line 25
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
       at ASP._Page_Views_Shared_Large_SearchLarge_cshtml.Execute() in c:\Sites\Website\Views\Shared\SearchLarge.cshtml:line 19
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

I'm setting the Sitecore context item so I'm confused why this happens. If I use @Html.Partial() and pass in the Model then it all works fine but I want to take advantage of the Sitecore cache by using the Html.Sitecore().Rendering() method instead.

Any ideas what might be causing this?

  • Can you try something like @using (Sitecore.Mvc.Common.ContextService.Get().Push(Html.ViewContext)) { Html.Sitecore().Rendering() }? – Marek Musielak Jan 14 at 18:51
  • @MarekMusielak you are a genius. Thats sorted it for me. Thanks a lot for your help. I could see from where the error was occurring that it looked like the render Pipeline didn't have the ViewContext from the html helper but wasn't sure how to pass it in. It needed the 'at symbol' to write out the view like so though: @Html.Sitecore().Rendering(). Do you want to add this as an answer? – Adam Seabridge Jan 15 at 10:42
1

From the exception it looks like it's ViewContext missing.

I'm not sure why it happens in your scenario but to fix this you can try the following code:

@using (Sitecore.Mvc.Common.ContextService.Get().Push(Html.ViewContext)) 
{ 
    @Html.Sitecore().Rendering()
}

It will make sure that ViewContext is pushed to the ContextService.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.