I want to display the current first and last name of ApplicationUsers Firstname + Lastname in my navigation bar in my _Layout view. I found that you can transfer your bag from the current RenderedBody controller as follows:
private readonly IHttpContextAccessor _httpContext;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ApplicationUser _user;
public HomeController(IHttpContextAccessor httpContextAccessor, UserManager<ApplicationUser> userManager) {
_httpContext = httpContextAccessor;
_userManager = userManager;
_user = _userManager.Users.Single(u => u.Id == _httpContext.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
}
And in the controller:
public IActionResult Index()
{
ViewBag.Username = $"{_user.FirstName} {_user.Surname}";
return View();
}
Finally, in my _Layout View:
<strong class="font-bold">@ViewBag.Username</strong>
This method seems to go against the grain and will be a huge pain for every point of view. What would be the standard in achieving this?
source
share