开发者

Html.Action() Causes a StackOverflowException

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开发者_StackOverflow社区
                {
                    <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?


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜