I'm unable to list all roles in my asp.net core 6 mvc web app. When entering the url path in the browser https://Localhost:44319/Admin/Role I recieve an 404 error. I followed the same steps for listing all users with no issues so I'm confused on why it's not working for roles. Any help would be much appreciated if one could review the code below.
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 >
<table >
<thead >
<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?}"
);
});
CodePudding user response:
In RoleController, add [Area("admin")]
[Area("admin")]
public class RoleController : Controller
{
public IActionResult Index()
{
return View();
}
}
Result: