C# ASP.NET默认值中的一个小更改\u LoginPartial视图不起作用
在我使用C# ASP.NET默认值中的一个小更改\u LoginPartial视图不起作用,c#,razor,visual-studio-2015,asp.net-core-mvc,asp.net-mvc-partialview,C#,Razor,Visual Studio 2015,Asp.net Core Mvc,Asp.net Mvc Partialview,在我使用个人用户帐户的ASP.NET核心项目中,我做了一个小小的更改,添加了一个角色检查,以显示注册链接(默认情况下,该链接显示在右上角),仅当用户已登录且处于管理员角色时才显示,以便只有管理员在登录后,也可以将其他用户添加到系统中。但由于某些原因,即使您以管理员身份登录,并且处于管理员角色,以下部分也不会显示注册链接。通过放置断点,我检查了User.IsInRole(“AdminRole”)的计算结果是否为true: \u LoginPartial: @model ABCTest.Models
个人用户帐户的ASP.NET核心项目中,我做了一个小小的更改,添加了一个角色检查,以显示注册
链接(默认情况下,该链接显示在右上角),仅当用户已登录且处于管理员角色时才显示,以便只有管理员在登录后,也可以将其他用户添加到系统中。但由于某些原因,即使您以管理员身份登录,并且处于管理员角色,以下部分也不会显示注册
链接。通过放置断点,我检查了User.IsInRole(“AdminRole”)
的计算结果是否为true:
\u LoginPartial:
@model ABCTest.Models.ABCTest.CurrentYear
@using Microsoft.AspNetCore.Identity
@using ABCTest.Models
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm">
<ul class="nav navbar-nav navbar-left">
<li><a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Welcome @UserManager.GetUserName(User), you are working with year @Model.CurrentYr - @(@Model.CurrentYr + 1)</a></li>
</ul>
<ul class="navbar-right" style="list-style:none;">
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
}
}
<li><button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button></li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
</ul>
}
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Add User</a></li>
}
}
@model ABCTest.Models.ABCTest.CurrentYear
@使用Microsoft.AspNetCore.Identity
@使用ABCTest.Models
@注入SignInManager SignInManager
@注入用户管理器用户管理器
@if(SignInManager.IsSignedIn(用户))
{
- 欢迎@UserManager.GetUserName(User),您正在使用year@Model.CurrentYr-@(@Model.CurrentYr+1)
}
其他的
{
- 登录
}
即使登录用户在AdminRole中也不显示的部分:
@model ABCTest.Models.ABCTest.CurrentYear
@using Microsoft.AspNetCore.Identity
@using ABCTest.Models
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm">
<ul class="nav navbar-nav navbar-left">
<li><a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Welcome @UserManager.GetUserName(User), you are working with year @Model.CurrentYr - @(@Model.CurrentYr + 1)</a></li>
</ul>
<ul class="navbar-right" style="list-style:none;">
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
}
}
<li><button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button></li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
</ul>
}
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Add User</a></li>
}
}
@{boolbtest=User.IsInRole(“AdminRole”);
如果(b测试)
{
添加用户
}
}
更新
我已验证用户Admin处于AdminRole
当我单步执行代码时,它会将我带到工具提示显示为true
的if(bTest)
部分,之后我可以看到光标转到if语句的{
部分,但随后它会跳到if语句的}
部分。但这可能是因为…
中没有c代码
更新2:
@model ABCTest.Models.ABCTest.CurrentYear
@using Microsoft.AspNetCore.Identity
@using ABCTest.Models
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm">
<ul class="nav navbar-nav navbar-left">
<li><a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Welcome @UserManager.GetUserName(User), you are working with year @Model.CurrentYr - @(@Model.CurrentYr + 1)</a></li>
</ul>
<ul class="navbar-right" style="list-style:none;">
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
}
}
<li><button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button></li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
</ul>
}
@{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Add User</a></li>
}
}
即使我硬编码if(bTest){…}
为if(true){…}
应用程序也不会显示寄存器
链接所以,我认为问题的关键在于:为什么应用程序忽略了if语句中的寄存器链接
最初我认为这与代码块中的内容有关,没有明确的@symbol就无法呈现。但这不应该发生在html标签上
然后我想标签可能已经被渲染了,并且被某种样式隐藏了,或者只是因为导航栏结构不正确而没有显示出来。原来它被OP案件的标志图片隐藏了 @JoshMein谢谢你的帮助。我在我的帖子中添加了一个UPDATE
部分来回答你的问题,我也会帮助其他人。试着去掉这个变量并执行@if(User.IsInRole(“AdminRole”){}@Steve谢谢你分享你的想法。我已经试过了。我甚至试着将if(bTest){…}
编码为if(true){…}
,但它不起作用。请查看我的更新2您可以查看dom元素并查看li是否确实没有呈现吗?@Steve谢谢您的建议。根据你的建议,我发现,…
实际上就在那里。它隐藏在一个图像后面(注意:这是_Layout.cshtml中的一个局部视图,我在其中放置了一个顶级公司徽标图像-我应该注意这一点)。所以我需要适当地调整视图内容。为了这篇文章的其他读者的兴趣,你可能想把你的评论转换成一个带有一点细节的回复,解释问题的可能原因。我会把它作为一个答案。