add and createroles in controller

48 views Asked by At
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");
}

when i write account/createrole/ in the url part in may localhost the roles can not be created. i didnt know why because i checked that for many times.

1

There are 1 answers

0
Eshana Milan Weerasinghe On

Ensure that you have set up the routing correctly in your Startup.cs file. It should map the URL path "account/createrole/" to the CreateRole action in your controller.

// Startup.cs

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

Make sure that the controller where this action correctly decorated with the [Controller] attribute.