我創建了新的控制器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");
用戶控制器.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;
}
}
uj5u.com熱心網友回復:
this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");
你有一個名為 的控制器fetchdata嗎?==>“api/fetchdata”
我猜你的意思是“api/users”,對吧?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371320.html
標籤:C# json asp.net核心 西装外套 blazor 服务器端
