【问题标题】:API Uri is not mapped to ControllerAPI Uri 未映射到控制器
【发布时间】:2021-11-30 12:24:40
【问题描述】:

我创建了新的控制器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;
    }
  }

【问题讨论】:

  • 您应该使用控制器上的 route 属性指定的路由:[Route("api/[controller]")] 表示 /api/Users,因为您的控制器名为 UsersController。所以在这种情况下,您需要在GetFromJsonAsync 调用中将/api/fetchdata 替换为/api/Users
  • [Route("api/[controller]")] 将路由指定为 "api/Users" 。因为您使用了 [controller],所以它使用了 Controller 的名称而不是“Controller”,因此是“Users”。
  • 解决方案是你们都已经提到的。但是,在修复了 api 路径之后,我仍然遇到了同样的问题。授权也有问题。所以我删除了数据库,应用了新的迁移,更新了数据库,然后创建了一个新用户 ang 再次登录。现在一切似乎都正常了!

标签: c# json asp.net-core blazor blazor-server-side


【解决方案1】:

this.applicationUserList = await this.httpClient.GetFromJsonAsync&lt;IEnumerable&lt;ApplicationUserDTO&gt;&gt;("api/fetchdata");

你有一个名为 fetchdata 的控制器吗? ==> "api/fetchdata"

我猜你的意思是“api/users”,对吧?

【讨论】:

  • 这是一个问题还是一个答案?
猜你喜欢
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2020-10-08
  • 2020-03-19
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多