For some reason, one of my Html.Action() methods is throwing a StackOverflowException which is only being caught when I debug the web server instance after it gets stuck and stops responding. All what I get is this:
System.StackOverflowException was unhandled
Cannot evaluate expression because the current thread is in a stack overflow state.
The line that is throwing the exception is this:
<div id="userInfoSummary">@Html.Action("Summary", "User")</div>
This happens when I login and then get redirected to the home page (which never happens because it gets stuck.
Here's how I check whether the user is logged in or not to render the view approriatly:
<div id="userPanel">
@if (!SessionManager.CheckSession(SessionKeys.User))
{
<div id="loginForm">@Html.Action("Login", "User")</div>
<div id="registerForm">@Html.Action("Register", "User")</div>
<hr class="greyLine" />
<div id="recentlyViewedItems">
<div id="recentItemsTitle">
<span class="recentItemsIcon"></span><span class="theRecentTitle">Recently Viewed</span>
</div>
</div>
}
else
{
<div id="userInfoSummary">@Html.Action("Summary", "User")</div>
}
</div>
And here are my ActionMethods:
[HttpPost]
public ActionResult Login(LoginViewModel dto)
{
bool flag = false;
if (ModelState.IsValid)
{
if (_userService.AuthenticateUser(dto.Email, dto.Password, false)) {
var user = _userService.GetUserByEmail(dto.Email);
var uSession = new UserSession
{
ID = user.Id,
Nickname = user.Nickname
};
SessionManager.RegisterSession(SessionKeys.User, uSession);
flag = true;
}
}
if (flag)
return RedirectToAction("Index", "Home");
else
{
ViewData.Add("InvalidLogin", "The login info you provided were incorrect.");
return View(dto);
}
}
public ActionResult Summary()
{
var user = _helper.GetUserFromSession();
var viewModel = Mapper.Map<User, UserInfoSummaryViewModel>(user);
return View(viewModel);
}
How can I get more information about this exception? And why is this happening in the first place? I do not think there are any recursive functions that are going endlessly or some infinite loop... Could it be that I'm calling several Html.Action() methods at the same time?
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. } } }
StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion. StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9.
Starting with the . NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow.
You shouldn't in any case be placing the Html.Action
call in the Summary
view or it will recursively call itself until you run out of stack. If the call is placed in the _Layout
of the site ensure that you are returning a partial view in the Summary
action so that this layout is not included:
public ActionResult Summary()
{
var user = _helper.GetUserFromSession();
var viewModel = Mapper.Map<User, UserInfoSummaryViewModel>(user);
return PartialView(viewModel);
}
or if you don't want to modify your Summary
controller action you could do the following in the Summary.cshtml
partial:
@{
Layout = null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With