我無法列出我的 asp.net core 6 mvc web 應用程式中的所有角色。在瀏覽器 https://Localhost:44319/Admin/Role 中輸入 url 路徑時,我收到 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?}"
);
});
uj5u.com熱心網友回復:
在 RoleController 中,添加[Area("admin")]
[Area("admin")]
public class RoleController : Controller
{
public IActionResult Index()
{
return View();
}
}
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/483858.html
