[英]How to get the current Id of a user to pass to _Layout ASP.NET Core?
I need to pass the Id of the current user to the _Layout master page.我需要将当前用户的 Id 传递给 _Layout 母版页。
It must be passed to where "asp-route-id"它必须传递到“asp-route-id”的位置
<a class="nav-link text-dark" asp-controller="Account" asp-action="MyPersonalCabinet" asp-route-id="HERE">My Cabinet</a>
Here is the method:这是方法:
public async Task<IActionResult> MyPersonalCabinet(string id)
{
var user = await _userManager.FindByIdAsync(id);
var userId = _userManager.GetUserId(HttpContext.User);
if (user != null && userId == id)
{
return View(user);
}
return NotFound();
}
With var userId = _userManager.GetUserId (HttpContext.User);
使用
var userId = _userManager.GetUserId (HttpContext.User);
I get the current id in the method.我在方法中得到了当前的 id。 How do I do something like this to pass this Id to _Layout
我该如何做这样的事情来将此 ID 传递给 _Layout
I tried to come up with something with ViewBag but it didn't work我试图用 ViewBag 想出一些东西,但它没有用
You can try injecting userManager in the layout.您可以尝试在布局中注入 userManager。
@using Microsoft.AspNetCore.Identity
@inject UserManager<ApplicationUser> userManager
Then retrieve Id然后检索Id
<p>@{var user = userManager.GetUserAsync(User);
var userId = user.Id;} </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.