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

问题描述 投票:0回答:1

请提问,如何增加在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>>();

并在构造函数中的问题是关闭的。 (谢谢亚历山大)

但鉴于问题

enter image description here

我的控制器没有任何错误,当我编译应用程序。然而,在返回查看I took it

私人IUserManagerRepository的UserManager;私人只读AppRoleManager UserRole的;

    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>

c# asp.net-mvc asp.net-identity ninject
1个回答
0
投票

为了解决这个错误,你必须做出RoleController构造函数及其参数类型一致的可访问性。由于构造函数是public它的参数必须是public为好。编译器抱怨AppRoleManager<IdentityRole>所以我们需要看看它。显然AppRoleManagerpublic,它看起来像IdentityRole只是不public在此处键入。因此,更新其声明

public class IdentityRole //
© www.soinside.com 2019 - 2024. All rights reserved.