Home > Mobile >  API Uri is not mapped to Controller
API Uri is not mapped to Controller

Time:12-01

I have created new controller UsersController.cs and trying to get access to it from FetchData.razor.cs, however there is no connection. I have figured this out by setting breakpoint at:

System.Collections.Generic.IEnumerable<ApplicationUser> applicationUsers = await _userManager.Users
            .Include(u => u.UserRoles)
                .ThenInclude(ur => ur.Role).ToListAsync();

and it is not getting called. Why so and how to fix that? Something wrong in this one, but I don't understand exactly what 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;
    }
  }

CodePudding user response:

this.applicationUserList = await this.httpClient.GetFromJsonAsync<IEnumerable<ApplicationUserDTO>>("api/fetchdata");

Do you have a controller named fetchdata ? ==> "api/fetchdata"

I guess you meant "api/users", right ?

  • Related