获取“ InvalidOperationException:无法解析类型服务”错误

尝试使用Identity Role Manager,.NET Core和Razor页面创建新角色时,出现“ InvalidOperationException:无法为类型解析服务”错误。

一旦试图访问该页面,它将引发错误。

由于似乎没有“ RoleManager”的接口,因此无法添加Transient服务

完全错误:

  

InvalidOperationException:无法解析类型的服务   'microsoft.AspNetCore.Identity.RoleManager`1 [microsoft.AspNetCore.Identity.IdentityRole]'   尝试激活“ iomra.Pages.createroleModel”时。

这是我的createroleModel页面的代码:

    using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using microsoft.AspNetCore.Identity;
using microsoft.AspNetCore.Mvc;
using microsoft.AspNetCore.Mvc.RazorPages;

namespace iomra.Pages
{
    public class createroleModel : PageModel
    {
        [Required]
        public string RoleName { get; set; }

        private RoleManager<IdentityRole> roleManager;
        public createroleModel(RoleManager<IdentityRole> roleMgr)
        {
            roleManager = roleMgr;
        }

        [HttpGet]
        public IactionResult CreateRole()
        {
            return RedirectToPage("/index");
        }


        [HttpPost]
        public async Task<IactionResult> Create()
        {
            if (ModelState.IsValid)
            {
                IdentityRole iR = new IdentityRole
                {
                    Name = RoleName
                };
                IdentityResult result = await roleManager.CreateAsync(iR);
                if (result.Succeeded)
                {
                    return RedirectToPage("/CreateRole");
                }
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddmodelError("",error.Description);
                }
            }

            return RedirectToPage("/createrole");
        }
    }
}

任何帮助将不胜感激。

zhanglin711 回答:获取“ InvalidOperationException:无法解析类型服务”错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3151905.html

大家都在问