在控制器中添加和创建角色

问题描述 投票:0回答:1
  public async Task<IActionResult> CreateRole()
        {

            foreach (UserRole item in Enum.GetValues(typeof(UserRole)))
            {
                if (await _roleManager.FindByNameAsync(item.ToString()) == null)
                {
                    await _roleManager.CreateAsync(new IdentityRole()
                    {
                        Name = item.ToString(),
                    });
                }
            }
            return RedirectToAction("Index", "Home");
        }

当我在 5 月 localhost 的 url 部分中写入 account/createrole/ 时,无法创建角色。我不知道为什么,因为我检查了很多次。

asp.net asp.net-mvc user-roles
1个回答
0
投票

确保您已在 Startup.cs 文件中正确设置路由。它应该将 URL 路径“account/createrole/”映射到控制器中的 CreateRole 操作。

//启动.cs

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

确保此操作所在的控制器正确地用 [Controller] 属性修饰。

© www.soinside.com 2019 - 2024. All rights reserved.