我需要为ROLES创建CRUD操作。
我收到以下错误:
“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务”
那么,我该如何注入roleManager?
我正在使用ASP Net Core 2.0 + Identity 2.2.1
类ApplicationUser
public class ApplicationUser : IdentityUser
{
[Key]
public override string Id { get; set; }
public bool Type { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在在Startup.cs中
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
.AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
控制者
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityUser> _roleManager;
public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityUser> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public IActionResult Index()
{
return View(_roleManager.Roles);
}
Run Code Online (Sandbox Code Playgroud)
因此,我收到错误消息:“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务。