在我的应用程序中,我在_LayoutPage.cshtml
中添加了一个导航栏。当我定义_NavigationBar.cshtml
部分时,它作为的局部视图从每个视图加载。我还添加了可以正常运行的Bootstrap 3,但是我尝试使用一些图标,并且由于我尝试应用字体和自定义字形,因此ASP.NET要么停留在加载页面上而没有成功,要么最终出现错误:
无法计算表达式,因为当前线程处于堆栈溢出状态
错误指出该行的navbar
string userrole = Model != null ? Model.UserRoles.First() : string.Empty;
下面的
_NavigationBar.cshtml
代码在更改后。我所做的是更改了线路:<li>@Html.ActionLink("Home", "Index", "Home")</li>
至
<li><a href="@Html.Action("Index", "Home")"><span class="glyphicon glyphicon-home"></span></a></li>
在导航栏中显示“主页”图标仍在工作。但是,当我尝试将第46行从
<li>@Html.ActionLink("Login", "Login", "Account")</li>
对此
<a href="@Html.Action("Login", "Account")"><span class="glyphicon glyphicon-log-in"> Login</span></a>
页面停止加载。
_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>
_NavigationBar.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>
最佳答案
@Html.Action()
调用服务器上的方法并返回结果。在您的情况下,该结果是一个视图,该视图包含@Html.Action()
然后被调用以返回该视图的另一个副本,该视图包含@Html.Action()
然后被调用以返回该视图的另一个副本,依此类推,直到您会收到一个stackoverflow异常。
你需要改变
<a href="@Html.Action("Login", "Account")">
至
<a href="@Url.Action("Login", "Account")">
这将生成一个URL
关于c# - 从Bootstrap添加自定义图标会导致“堆栈溢出状态”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28407064/