【问题标题】:Get the list of Roles in ASP.NET MVC获取 ASP.NET MVC 中的角色列表
【发布时间】:2016-06-20 16:29:57
【问题描述】:

我有以下方法来获取存储在AspNetRoles中的角色列表

    [AllowAnonymous]
    public async Task<ActionResult> Register()
    {
        //Get the list of Roles
        ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");

        return View();
    }

然后我得到它如下视图

    <div class="form-group">
        <label class="col-md-2 control-label">
            Select User Role
        </label>
        <div class="col-md-10">
            @foreach (var item in (SelectList)ViewBag.RoleId)
            {
                <input type="checkbox" name="SelectedRoles" value="@item.Value" class="checkbox-inline" />
                @Html.Label(item.Value, new { @class = "control-label" })
            }
        </div>
    </div>

但是一旦我加载页面,我就会收到Object reference not set to an instance of an object. 错误

第 175 行:ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "名称", "名称");

这是RoleManager定义

    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _roleManager = value;
        }
    }

这是ApplicationRoleManager 模特

// Configure the RoleManager used in the application. RoleManager is defined in the ASP.NET Identity core assembly
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
    }
}

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-identity role-manager


【解决方案1】:

在 Startup.Auth 中,像这样引用 RoleManager:

    public void ConfigureAuth(IAppBuilder app)
    {
        // Add this reference
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    }

确保你的 Controller 包含这个构造函数:

        // Include this
        private ApplicationRoleManager _roleManager;
        // You already have this
        public ApplicationRoleManager RoleManager { get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); } private set { _roleManager = value; } }

重建,再试一次,希望这能解决问题。

【讨论】:

  • 感谢您的指导,一旦我这样做了,我的所有其他表格都不见了:(
  • @kez - 我不明白您所说的“所有其他表格都消失了”是什么意思..?
  • 它删除了我所有的表,只保留了 ASpNet 身份表,另一件事是,一旦我将包含默认 aspnet 身份表的新表添加到这个数据库中,然后我调试程序它会删除所有新添加的表,并删除了 asnet 身份表中的所有数据,如何避免这种情况
  • @kez - 添加指示的行不会取出数据库表,它们只是引用 RoleManager。请检查您是否只是复制和粘贴 ConfigureAuth,因为(可能导致它的一件事)它还应该包括: app.CreatePerOwinContext(ApplicationDbContext.Create);可能还有其他事情。如果您使用代码优先,则重建生成表也可能是另一个问题。请检查您是否添加指示的行,但保持其他代码不变。
【解决方案2】:

检查您的 RoleManager.Roles 属性。似乎它没有从数据库返回任何东西。还要确保UserRoles 表中至少有一个角色实体

【讨论】:

  • 在哪里可以找到该属性,在 IdentityModel.cs 中?
  • @kez 做一个 var test = RoleManager.Roles;并调试看看你得到了什么。
  • 是的,它的 null ,然后如何加载值
  • @kez,如果您真的想从数据库中获取所有角色,您可以使用经理的GetRolesAsync() 方法。它返回的结果与使用 Roles 属性相同。
  • 我遇到了同样的问题,当我查看Identity 代码时,我发现框架试图返回IQueryable&lt;T&gt; 的实例,这是导致其中的内部错误
猜你喜欢
  • 2011-12-11
  • 2021-05-24
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 2015-07-17
  • 1970-01-01
相关资源
最近更新 更多