【发布时间】:2016-12-14 16:01:39
【问题描述】:
我想在我的 viewComponent 中获得对 applicationUser 的访问权限。但是,这不像从“Controller”继承的普通类。
有谁知道我如何从我的 ViewComponent 访问 ApplicationUser?
public class ProfileSmallViewComponent : ViewComponent
{
private readonly ApplicationDbContext _Context;
private readonly UserManager<ApplicationUser> _UserManager;
public ProfileSmallViewComponent(UserManager<ApplicationUser> UserManager, ApplicationDbContext Context)
{
_Context = Context;
_UserManager = UserManager;
}
//GET: /<controller>/
public async Task<IViewComponentResult> InvokeAsync()
{
//ApplicationUser CurrentUser = _Context.Users.Where(w => w.Id == _UserManager.GetUserId(User)).FirstOrDefault();
//Code here to get ApplicationUser
return View("Header");
}
}
【问题讨论】:
-
"这不像从 "Controller" 继承的普通类。"。你能更明确地说为什么这个“不起作用”吗?你有例外吗?如果是这样,请将异常详细信息添加到您的问题中。
标签: c# asp.net-core dependency-injection asp.net-core-identity asp.net-core-viewcomponent