Html.Action()使StackOverflowExceptionHtml、Action、StackOverflowException

2023-09-04 09:14:27 作者:不及半分昧

由于某些原因,我Html.Action的()方法之一是扔它,当我调试Web服务器实例只被抓到后,被卡住并停止响应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是   未处理的

System.StackOverflowException was unhandled

无法计算EX pression因为   当前线程堆栈溢出   状态。

Cannot evaluate expression because the current thread is in a stack overflow state.

这抛出异常的行是这样的:

The line that is throwing the exception is this:

< D​​IV ID =userInfoSummary> @ Html.Action(摘要,用户)< / DIV>

这发生在我登录,然后重定向到主页(这永远不会发生,因为它被卡住。

This happens when I login and then get redirected to the home page (which never happens because it gets stuck.

下面是我如何检查用户是否登录或不approriatly渲染视图:

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>

这是我的ActionMethods:

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

我如何获得有关此异常的更多信息?为什么会出现这种情况摆在首位?我不认为有这打算不休任何递归函数或一些无限循环......难道是我打电话几个Html.Action()方法,在同一时间?

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?

推荐答案

您不应该在任何情况下被放置在摘要视图或将递归调用自身直到用完栈。如果呼叫被放置在 _layout 网​​站确保您正在返回的摘要的局部视图操作,以便该布局不包括:

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

如果你不希望修改摘要控制器操作,你可以做的 Summary.cshtml 部分:

or if you don't want to modify your Summary controller action you could do the following in the Summary.cshtml partial:

@{
    Layout = null;
}