Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore controller rendering causing StackOverflowException

I am trying to do a simple controller rendering with Sitecore 8 and for some reason it's producing a StackOverflowException on the line within the main layout markup that contains the reference to the placeholder it is to be rendered in. This seems to crash the worker process, but you can see the stack overflow on debugging the process:

w3p crashStackOverflow

Here is my very basic controller:

Controller/View

And here is my controller rendering definition:

enter image description here

Reproduction notes:

  1. This is occurring in a vanilla Sitecore 8 installation (rev. 150427 -installed via SIM).
  2. The MVC project is also vanilla -created with empty ASP.NET project, then NuGetting in MVC 5.1.
  3. Web.config & Global added to project from the Sitecore site root in wwwroot.

FYI - everything is absolutely fine doing a view rendering - it's just controller renderings that seem to be causing a problem

like image 306
David Masters Avatar asked Jun 23 '15 10:06

David Masters


People also ask

What causes StackOverflowException?

A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }

How do I stop StackOverflowException?

Starting with the . NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.

What is controller rendering in Sitecore?

A Sitecore Controller Rendering is more complex than a View Rendering, mainly in that it requires a controller action. The rendering definition item includes both a controller name and controller action to execute. The controller action is then responsible for returning the correct view.

How do I get system StackOverflowException?

Once you have the settings open, expand 'Common Language Runtime Exceptions', expand 'System', scroll down and check 'System. StackOverflowException'. Then you can look at the call stack and look for the repeating pattern of calls.


Video Answer


2 Answers

So the problem was actually pretty simple in the end.

Returning a ViewResult when the view is intended as a partial view (which all Sitecore renderings will be) then you must set the layout property in the markup to null:

@{
    Layout = null;
}

Otherwise MVC will try to wrap the layout file around it, which of course contains your Sitecore placeholder, which causes an infinite loop and crashes the worker process with a StackOverflowException.

So in the context of Sitecore, either return a PartialViewResult or return a ViewResult with the layout set as null.

like image 122
David Masters Avatar answered Sep 28 '22 09:09

David Masters


I guess there is something missing in placeholder setting, could you check in path sitecore/layout/placeholder setting?

There should be a placeholder key which you are trying to use.

Hope this will help

Cheers!!

like image 36
Nishant Jichkar Avatar answered Sep 28 '22 10:09

Nishant Jichkar