Sitecore MVC: StackOverflowException when returning views instead of partial views

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

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.