获取 ASP .Net Core WebAPI 中的所有身份角色
Get all Identity roles in ASP .Net Core WebAPI
我的任务是获取数据库中的所有身份角色。我使用下面的代码来获取所有角色。在 Asp.Net 核心 WebAPI
UserRoleService.cs
public class UserRoleService
{
private readonly RoleManager<IdentityRole> _roleManager;
public UserRoleService(RoleManager<IdentityRole> roleManager)
{
_roleManager=roleManager;
}
public Task<IList<string>> AllUserRoles(){
return _roleManager.Roles;
}
}
但是我得到以下错误
Cannot implicitly convert type
'System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>' to
'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>'.
An explicit conversion exists (are you missing a cast?)
请给我一个解决这个错误的方法
当我把它改成
Task<List<IdentityRole>> AllUserRoles()
{
return _roleManager.Roles;
}
我遇到了这样的错误
Cannot implicitly convert type
'System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>' to
'System.Threading.Tasks.Task<System.Collections.Generic.List<Microsoft.AspNetCore.Identity.IdentityRole>>'.
An explicit conversion exists (are you missing a cast?)
我认为问题出在您在那里使用的 return 类型。
根据您的代码,我假设 _roleManager.Roles 是 System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>
.
类型
您的 return 类型改为 Task<List<IdentityRole>>
或 Task<IList<string>>
您可以将函数的 return 类型更改为 IQueryable,如下所示:
public List<IdentityRole> AllUserRoles()
{
return _roleManager.Roles.ToList();
}
或者做类似的事情:
public Task<List<IdentityRole>> AllUserRoles()
{
return Task.FromResult(_roleManager.Roles.ToList());
}
我的任务是获取数据库中的所有身份角色。我使用下面的代码来获取所有角色。在 Asp.Net 核心 WebAPI
UserRoleService.cs
public class UserRoleService
{
private readonly RoleManager<IdentityRole> _roleManager;
public UserRoleService(RoleManager<IdentityRole> roleManager)
{
_roleManager=roleManager;
}
public Task<IList<string>> AllUserRoles(){
return _roleManager.Roles;
}
}
但是我得到以下错误
Cannot implicitly convert type
'System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>' to
'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>'.
An explicit conversion exists (are you missing a cast?)
请给我一个解决这个错误的方法
当我把它改成
Task<List<IdentityRole>> AllUserRoles()
{
return _roleManager.Roles;
}
我遇到了这样的错误
Cannot implicitly convert type
'System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>' to
'System.Threading.Tasks.Task<System.Collections.Generic.List<Microsoft.AspNetCore.Identity.IdentityRole>>'.
An explicit conversion exists (are you missing a cast?)
我认为问题出在您在那里使用的 return 类型。
根据您的代码,我假设 _roleManager.Roles 是 System.Linq.IQueryable<Microsoft.AspNetCore.Identity.IdentityRole>
.
您的 return 类型改为 Task<List<IdentityRole>>
或 Task<IList<string>>
您可以将函数的 return 类型更改为 IQueryable,如下所示:
public List<IdentityRole> AllUserRoles()
{
return _roleManager.Roles.ToList();
}
或者做类似的事情:
public Task<List<IdentityRole>> AllUserRoles()
{
return Task.FromResult(_roleManager.Roles.ToList());
}