我不明白如何在我的应用程序 MVC 中通过 Ninject 创建 RoleManager

I don't understand how create RoleManager by Ninject in my app, MVC

请问,如何在Ninject.Web.Common中添加绑定?

我有角色的实体

public class AppRole : IdentityRole
{
    public AppRole() : base() { }

    public AppRole(string name)
        : base(name)
    { }
}

和经理

public class AppRoleManager : RoleManager<AppRole>
{
    public AppRoleManager(RoleStore<AppRole> store) : base(store)
    { }
}

我希望创建方法在 Ninject 中执行,就像我在 UserManager 和 SignUpManager 中执行的那样

我的 Ninject 普通的

private static void RegisterServices(IKernel kernel)
    {
        System.Web.Mvc.DependencyResolver.SetResolver(new Infrastructure.NinjectDependencyResolver(kernel));

        kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
        kernel.Bind<RoleManager<IdentityRole, string>>().ToSelf();

    }

kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
    kernel.Bind<RoleManager<IdentityRole, string>>().ToSelf();

我将错误的绑定更改为

kernel.Bind<AppRoleManager>().ToSelf();
        kernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();

并且构造函数中的问题已解决。 (谢谢亚历山大)

但是看到的问题

我的控制器在编译应用程序时没有任何错误。但是在 return 视图中

私有 IUserManagerRepository 用户管理器; 私人只读 AppRoleManager 用户角色;

    public RoleController(IUserManagerRepository userManager, AppRoleManager userRole)
    {
        this.userManager = userManager;
        this.userRole = userRole;
    }

    // GET: Role
    public ActionResult Index()
    {
        return View(userRole.Roles);
    }

还有我的看法

@using Domain.IdentityManager
@using Domain.Entities
@model IEnumerable<AppRole>

<div class="panel panel-primary">
<div class="panel-heading">Roles</div>
<table class="table table-striped">
    <tr>
        <th>ID</th>
        <th>Название</th>
        <th>Пользователи</th>
        <th style="min-width: 150px"></th>
    </tr>
        @foreach (AppRole role in Model)
        {
            <tr>
                <td>@role.Id</td>
                <td>@role.Name</td>
                <td>
                    @if (role.Users == null || role.Users.Count == 0)
                    {
                        @: Нет пользователей в этой роли
                    }
                    else
                    {

                }
                </td>
        }

</table>

要修复此错误,您必须使 RoleController 构造函数及其参数类型的 一致的可访问性 。由于构造函数是 public 它的参数也必须是 public 。编译器抱怨 AppRoleManager<IdentityRole> 所以我们需要看看它。显然 AppRoleManagerpublic,看起来 IdentityRole 只是不是 public 类型。所以更新它的声明

public class IdentityRole //