【问题标题】:MVC: An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dllMVC:System.Core.dll 中出现“System.StackOverflowException”类型的未处理异常
【发布时间】:2014-01-30 13:22:22
【问题描述】:

这是我应用的主要布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Date Picker", "Index", "Home", null, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")

            </div>
        </div>
    </div>

    @if (User.Identity.IsAuthenticated)
    {
        <div class="row">
            <div class="col-md-3">
                <div class="bs-sidebar hidden-print affix" role="complementary">
                    <ul class="nav bs-sidenav">                       
                        <li class="active">
                            @Html.ActionLink("Address Book","Index","AddressBook")
                            <ul class="nav">                              
                                <li>
                                    @Html.ActionLink("Add Contact", "AddPerson", "AddressBook")
                                </li>                               
                            </ul>
                        </li>
                        <li>
                            @Html.ActionLink("App", "Create", "Appointment")                           
                        </li>
                        <li>
                            @Html.ActionLink("myconn", "Index", "Connection")//error when I click this link.                     
                        </li>
                    </ul>
                </div>
            </div>
        </div>      
    }
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - MyApp</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

当用户登录时,我得到了侧边栏,所有链接都在侧边栏中工作,除了

@Html.ActionLink("myconn", "Action", "Controller")   

当我单击名为 SuperOffice 浏览器的链接时,链接更改为 http://localhost:14834/Connection 但我得到错误:在视觉工作室说

An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll

这是我的控制器代码

[Authorize]
public class Controller : Controller
{
    private readonly IConnectionRepository _connectionRepository;

    public ConnectionController(IConnectionRepository connectionRepository)
    {
        _connectionRepository = connectionRepository;
    }
}

当我在 Connection 控制器的 Index 方法中放置断点并单击 SuperOffice 链接时,我什至没有进入该方法。 知道发生了什么吗? 我觉得奇怪的是所有链接都在工作,我被转发到控制器,除了那个之外,所有东西都运行良好。

【问题讨论】:

  • 在类构造函数中放一个断点,代码中某处有一些循环引用。
  • 我的控制器类或存储库类?
  • 你的控制器类
  • @JeremyHolovacs 我从未被转发到该控制器,当我在浏览器中单击该链接时应用程序中断。我在该类的第一行有断点,但从未被调用
  • 您的帐户控制器类不应具有Authorized 属性。尝试删除它,看看是否有影响。

标签: c# asp.net-mvc compiler-errors stack-overflow


【解决方案1】:

你可能在某处有[Authorize] 属性,这给了你stackoverflow。

【讨论】:

  • 不,它来自我的存储库,显然我不能有两个存储库,在构造函数中相互实例化。
【解决方案2】:
    var hasSuperOfficeConnection = _connectionRepository.CheckIfSuperOfficeIsConnected(userId);
    var model = new Index
    {
        IsSuperOfficeConnected = hasSuperOfficeConnection
    };

我认为这里有一个无限循环如果您可以提供更多代码(即 IsSuperOficeConnected)我们可以为您提供更多帮助

public class ConnectionRepository:IConnectionRepository
{
    private readonly DatePickerDbContext _db;
    //private readonly ISuperOfficeRepository _superOfficeRepository;

    public ConnectionRepository(DatePickerDbContext db)//, ISuperOfficeRepository superOfficeRepository
    {
        _db = db;
      //  _superOfficeRepository = superOfficeRepository;
    }
     public int GetConnectionId(string connectionName)
     {
        var connection = _db.Connections.Single(c => c.Name == connectionName);
        return connection.Id;
      }

     public bool CheckIfSuperOfficeIsConnected(string userId)
     {
        var connectionId = GetConnectionId("SuperOffice");
        var result = _db.UserConnections.Where(uc => uc.UserId == userId && uc.ConnectionId == connectionId);
        return result.Any();
     }
 }

【讨论】:

  • _connectionRepository 调用 _superOfficeRepository 和 _superOfficeRepository 调用 _connectionRepository 导致无限循环
  • ConnectionRepository中是否需要superOfficeRepository?如果不只是删除它,可能会解决问题
  • 其实我需要它。但也许我可以合并它们
  • 你在哪里使用 superOfficeRepository?在给定的代码中没有看到
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多