[英]ASP .NET Core stuck loading view component in partial view
I'm working on an ASP .NET Core application, and I'm trying to load a view component inside of _LoginPartial.我正在开发一个 ASP .NET Core 应用程序,我正在尝试在 _LoginPartial 中加载一个视图组件。 _LoginPartial was created automatically when I created the new project, and my view component is called UserNameViewComponent. _LoginPartial 是我新建项目时自动创建的,我的视图组件叫做 UserNameViewComponent。 The view component should get the logged-in person's username and display it at the top of the screen.视图组件应该获取登录人的用户名并将其显示在屏幕顶部。
Whenever I log in, the page application gets stuck on the login screen until I stop it.每当我登录时,页面应用程序都会卡在登录屏幕上,直到我停止它。 I added a breakpoint to the line in _LoginPartial that calls the view component.我在 _LoginPartial 中调用视图组件的行添加了一个断点。 When the view component is called it gets my username successfully, but then when it goes back to _LoginPartial it runs the same line of code again to go back to the view component, and this happens forever.当视图组件被调用时,它成功获取了我的用户名,但是当它返回到 _LoginPartial 时,它再次运行相同的代码行以返回到视图组件,并且这种情况永远发生。
Block of code in _LoginPartial where view component is called: _LoginPartial 中调用视图组件的代码块:
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-light" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">@await Component.InvokeAsync("UserName")</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
<button type="submit" class="nav-link btn btn-link text-light">Logout</button>
</form>
</li>
}
UserNameViewComponent:用户名视图组件:
namespace Tangy.ViewComponents
{
public class UserNameViewComponent : ViewComponent
{
private readonly ApplicationDbContext _db;
public UserNameViewComponent(ApplicationDbContext db)
{
_db = db;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var claimsIdentity = (ClaimsIdentity)this.User.Identity;
var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
var userFromDb = await _db.Users.Where(u => u.Id == claim.Value).FirstOrDefaultAsync();
return View(userFromDb);
}
}
Default.cshtml:默认.cshtml:
@model Tangy.Models.ApplicationUser
@{
ViewData["Title"] = "Default";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Hi @Model.FirstName @Model.LastName <i class="far fa-user"></i>
Does anyone know what I'm doing wrong here?有谁知道我在这里做错了什么?
It looks like the issue is that _LoginPartial
is contained in _Layout
and rendering your View Component which then references _Layout
.看起来问题在于_LoginPartial
包含在_Layout
并呈现您的视图组件,然后引用_Layout
。 This will create a circular reference and the code for your view component (and the code for _Layout
) will be called over and over.这将创建一个循环引用,并且您的视图组件的代码(以及_Layout
的代码)将被一遍_Layout
调用。
_Layout
-> _LoginPartial
-> Default.cshtml
(from view component) -> _Layout
-> _LoginPartial
-> Default.cshtml
(from view component ) on and on and on..... _Layout
-> _LoginPartial
-> Default.cshtml
(来自视图组件)-> _Layout
-> _LoginPartial
-> Default.cshtml
(来自视图组件)不断.....
setting the Layout to null in the ViewComponent's view should solve your issue.在 ViewComponent 的视图中将 Layout 设置为 null 应该可以解决您的问题。
@{
ViewData["Title"] = "Default";
Layout = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.