14

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?

Kassem
  • 8,116
  • 17
  • 75
  • 116

1 Answers1

39

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;
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Damn! This keeps happening to me! I always return a view rather than returning a PartialView! Thanks for spotting that Darin, I really appreciate it. On a side note, in the Login action method, if the ModelState was in valid, what should I be returning? A View or a PartialView (this failed actually) in order to display the errors to the user... – Kassem Apr 10 '11 at 17:11
  • @Kassem, it's difficult to say without more details on your scenario. Maybe you could start a new question by providing more details? – Darin Dimitrov Apr 10 '11 at 17:14
  • Sure. I'll do that when I get to it. Thanks a lot :) – Kassem Apr 10 '11 at 17:17
  • I don't know why the hell I am having trouble with this. I have `return PartialView(viewModel);`, `@{ Layout = null; }`. The code also _runs on my computer_. It is only happening after publishing. I also have other `@Html.Actions` that are working without any problems. – JonathanPeel Dec 05 '16 at 21:49
  • I am also using `ChildActionOnly`. – JonathanPeel Dec 05 '16 at 21:49
  • Okay, I was calling an Async method, and that seemed to be causing problems. I am still not sure why it would have worked locally. – JonathanPeel Dec 05 '16 at 22:51
  • any idea about this one ? https://stackoverflow.com/questions/60136701/system-stackoverflowexception-after-httppost-returning-view-asp-mvc – keyone2693 Feb 10 '20 at 11:24