无法列出所有角色
Unable to List all Roles
我无法列出我的 asp.net 核心 6 mvc 网络应用程序中的所有角色。在浏览器中输入 url 路径时 https://Localhost:44319/Admin/Role 我收到 404 错误。我按照相同的步骤列出所有没有问题的用户,所以我很困惑为什么它不适用于角色。如果可以查看下面的代码,我们将不胜感激。
using System.ComponentModel.DataAnnotations;
namespace MyWebApp.Areas.Admin.ViewModels
{
public class RoleVM
{
[Required]
[Display(Name = "Role")]
public string? RoleName { get; set; }
}
}
namespace MyWebApp.Areas.Admin.Controllers
{
public class RoleController : Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
public RoleController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
public IActionResult Index()
{
var roles = _roleManager.Roles;
return View(roles);
}
}
}
@model IEnumerable<Microsoft.AspNetCore.Identity.IdentityRole>
@{
ViewData["Title"] = "All Roles";
}
<div class="table-responsive">
<table class="table table-hover text-secondary">
<thead class="text-dark">
<tr>
<th>Role</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var role in Model)
{
<tr>
<td>@role</td>
<td>
<partial name="_RUDButtonsPartial" model="@role.Id">
</td>
</tr>
}
</tbody>
</table>
</div>
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
}).AddEntityFrameworkStores<ApplicationDbContext>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Admin",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
在 RoleController 中,添加 [Area("admin")]
[Area("admin")]
public class RoleController : Controller
{
public IActionResult Index()
{
return View();
}
}
结果:
我无法列出我的 asp.net 核心 6 mvc 网络应用程序中的所有角色。在浏览器中输入 url 路径时 https://Localhost:44319/Admin/Role 我收到 404 错误。我按照相同的步骤列出所有没有问题的用户,所以我很困惑为什么它不适用于角色。如果可以查看下面的代码,我们将不胜感激。
using System.ComponentModel.DataAnnotations;
namespace MyWebApp.Areas.Admin.ViewModels
{
public class RoleVM
{
[Required]
[Display(Name = "Role")]
public string? RoleName { get; set; }
}
}
namespace MyWebApp.Areas.Admin.Controllers
{
public class RoleController : Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
public RoleController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
public IActionResult Index()
{
var roles = _roleManager.Roles;
return View(roles);
}
}
}
@model IEnumerable<Microsoft.AspNetCore.Identity.IdentityRole>
@{
ViewData["Title"] = "All Roles";
}
<div class="table-responsive">
<table class="table table-hover text-secondary">
<thead class="text-dark">
<tr>
<th>Role</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var role in Model)
{
<tr>
<td>@role</td>
<td>
<partial name="_RUDButtonsPartial" model="@role.Id">
</td>
</tr>
}
</tbody>
</table>
</div>
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15);
}).AddEntityFrameworkStores<ApplicationDbContext>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "Admin",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
在 RoleController 中,添加 [Area("admin")]
[Area("admin")]
public class RoleController : Controller
{
public IActionResult Index()
{
return View();
}
}
结果: