If you end up getting a StackOverflowException in one of your controllers when using MVC with Sitecore it might be because you are returning View() instead of PartialView().

If you want to return View() in your controller then you either need to set layout to null/empty string on the view itself or add it to _ViewStart.cshtml - otherwise you will end up with an infinite loop.

@{
    Layout = "";
}

An alternative is to make sure you don't have a _ViewStart.cshtml file with your main layout specified.

The loop happens because of placeholders in the main layout file. If one of the renderings in a placeholder returns a view with View() - instead of a partial view with PartialView() - then it will try to load the layout file, which will then load the renderings in the placeholders, which will then load the layout file and so on.

As mentioned to avoid this issue you can just return PartialView() instead as that does not try to load the layout again.