Celdor
Celdor

Reputation: 2597

Adding custom icons from the Bootstrap causes "stack overflow state" error

In my application, I have added a navigation bar in _LayoutPage.cshtml. It is loaded as a partial view from _NavigationBar.cshtml into each View when I define the section navbar. I also added Bootstrap 3 which works fine but I have tried to use a few icons and since I have tried applying fonts and custom glyphs, the ASP.NET either stays on loading page without success or eventually got error:

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

Th error point out _NavigationBar.cshtml at the line

string userrole = Model != null ? Model.UserRoles.First() : string.Empty;

The code for _NavigationBar.cshtml below is after changes. What I did was I changed the line:

<li>@Html.ActionLink("Home", "Index", "Home")</li>

to

<li><a href="@Html.Action("Index", "Home")"><span class="glyphicon glyphicon-home"></span></a></li>

It was still working showing the icon Home in navigation bar. However, when I try to change line 46 from

<li>@Html.ActionLink("Login", "Login", "Account")</li>

to this

<a href="@Html.Action("Login", "Account")"><span class="glyphicon glyphicon-log-in"> Login</span></a>

The page stopped loading.


Code for _NavigationBar.cshtml

@using RaDAR_MVC4_EF6.ViewModels.Account
@model UserData
@{
    string identityName = Model != null ? Model.UserName : string.Empty;
    string userrole = Model != null ? Model.UserRoles.First() : string.Empty;
}

<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
        <a class="navbar-brand">RaDAR</a>
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#radar-header-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        </div>
        <div class="collapse navbar-collapse" id="radar-header-collapse">
            <ul class="nav navbar-nav">
                <li><a href="@Html.Action("Index", "Home")"><span class="glyphicon glyphicon-home"></span></a></li>
                <li>@Html.ActionLink("Patients", "Index", "Patients")</li>
                <li>@Html.ActionLink("Applicants", "Index", "Applicants")</li>
                <li>@Html.ActionLink("Applications", "Index", "Applications")</li>
                <li>@Html.ActionLink("Accounts", "Index", "Account")</li>
            </ul>
            @if (Model != null)
            {
                <ul class="nav navbar-nav navbar-right">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Signed as @identityName (@userrole)</a>
                        <ul  class="dropdown-menu">
                            <li>@Html.ActionLink("Change password", "UpdatePassword", "Account", new { UserID = Model.UserID }, null)</li>
                        </ul>
                    </li>
                    <li>
                        @Html.ActionLink("Logout", "Logout", "Account", new { userName = identityName }, null)
                    </li>
                </ul>
            }
            else
            {
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="@Html.Action("Login", "Account")"><span class="glyphicon glyphicon-home"></span> Login</a>
                    </li>
                </ul>
            }
        </div>
    </div>
</nav>

Code for _Layout.cshtml

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="@Url.Content("~/Content/css/bootstrap.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/css/bootstrap-theme.min.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/StyleSheet.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-2.1.3.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/js/bootstrap.min.js")" type="text/javascript"></script>
    <title>@ViewBag.Title</title>
</head>

<body>
@if (IsSectionDefined("navbar"))
{
    @RenderSection("navbar")
}

<div class="container">
    @RenderBody()
</div>

@if (IsSectionDefined("footer"))
{
<div class="container">
    @RenderSection("footer")
</div>
}
</body>
</html>

Upvotes: 3

Views: 621

Answers (1)

user3559349
user3559349

Reputation:

@Html.Action() calls a method on your server and returns the result. In your case, that result is a view, which contains @Html.Action() which is then called to return another copy of the view, which contains @Html.Action() which is then called to return another copy of the view and so on and so on until you get a stackoverflow exception.

You need to change

<a href="@Html.Action("Login", "Account")">

to

<a href="@Url.Action("Login", "Account")">

which will generate a url

Upvotes: 1

Related Questions