API URI 未映射到控制器
API Uri is not mapped to Controller
我创建了新控制器 UsersController.cs
并尝试从 FetchData.razor.cs
访问它,但是没有连接。我通过在以下位置设置断点来解决这个问题:
System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role).ToListAsync();
它没有被调用。为什么会这样以及如何解决这个问题?
这个有问题,但我不明白到底是什么api/fetchdata
?
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
UsersController.cs:
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IMapper _mapper;
private List<ApplicationUserDTO> applicationUsersDTO = new List<ApplicationUserDTO>();
public UsersController(ApplicationDbContext context, UserManager<ApplicationUser> userManager, IMapper mapper)
{
this._context = context;
this._userManager = userManager;
this._mapper = mapper;
}
[HttpGet]
public async Task<IActionResult> Get()
{
System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role).ToListAsync();
this.applicationUsersDTO = applicationUsers
.Select(person => new ApplicationUserDTO
{
Id = person.Id,
FirstName = person.FirstName,
Email = person.Email
}).ToList();
//ApplicationUserDTO applicationUsersDTO = _mapper.Map<ApplicationUserDTO>(applicationUsers);
return Ok(applicationUsersDTO);
}
}
FetchData.razor.cs:
public partial class FetchData
{
[Inject]
public HttpClient httpClient { get; set; }
public IEnumerable<ApplicationUserDTO> applicationUserList { get; set; }
protected bool isLoaded = false;
public FetchData()
{
}
protected async override Task OnInitializedAsync()
{
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
this.isLoaded = true;
}
}
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
您有一个名为 fetchdata
的控制器吗? ==>“api/fetchdata”
我猜你的意思是“api/users”,对吧?
我创建了新控制器 UsersController.cs
并尝试从 FetchData.razor.cs
访问它,但是没有连接。我通过在以下位置设置断点来解决这个问题:
System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role).ToListAsync();
它没有被调用。为什么会这样以及如何解决这个问题?
这个有问题,但我不明白到底是什么api/fetchdata
?
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
UsersController.cs:
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IMapper _mapper;
private List<ApplicationUserDTO> applicationUsersDTO = new List<ApplicationUserDTO>();
public UsersController(ApplicationDbContext context, UserManager<ApplicationUser> userManager, IMapper mapper)
{
this._context = context;
this._userManager = userManager;
this._mapper = mapper;
}
[HttpGet]
public async Task<IActionResult> Get()
{
System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
.Include(u => u.UserRoles)
.ThenInclude(ur => ur.Role).ToListAsync();
this.applicationUsersDTO = applicationUsers
.Select(person => new ApplicationUserDTO
{
Id = person.Id,
FirstName = person.FirstName,
Email = person.Email
}).ToList();
//ApplicationUserDTO applicationUsersDTO = _mapper.Map<ApplicationUserDTO>(applicationUsers);
return Ok(applicationUsersDTO);
}
}
FetchData.razor.cs:
public partial class FetchData
{
[Inject]
public HttpClient httpClient { get; set; }
public IEnumerable<ApplicationUserDTO> applicationUserList { get; set; }
protected bool isLoaded = false;
public FetchData()
{
}
protected async override Task OnInitializedAsync()
{
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
this.isLoaded = true;
}
}
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
您有一个名为 fetchdata
的控制器吗? ==>“api/fetchdata”
我猜你的意思是“api/users”,对吧?