Blazor Webassembly API Call Children could not be evaluated

倖福魔咒の 提交于 2021-01-03 06:27:43

问题


I'm trying to call an existing rest API I have with my new Blazor Webassembly app, however when I make the call I always get the error Children could not be evaluated. The API hits the breakpoint and when I step through it returns the data successfully which I can also see if I call it in postman, however the Blazor call fails. I thought perhaps it was an issue with mismatching classes, however the class structure for TeamModel is identical as it is now a shared resource. I'm new with Blazor and I'm sure I'm missing something but could do with some direction or help.

private async Task FetchTeamData()
{
        try
        {
            List<TeamModel> teams;
            teams = await Http.GetFromJsonAsync<List<TeamModel>>("http://localhost:50663/api/team");
        }
        catch (Exception ex)
        {
            //Always blowing up with Children could not be evaluated here.
        }
}
[HttpGet]
public IActionResult GetAllTeams()
{
    List<TeamModel> teams = _teamRepository.GetAllTeams();

    return Ok(teams);
}

Edit: Well I've narrowed it down to being something wrong with the way the project is reading that API. I can call other hosted APIs fine. I'm not sure if its because its localhost or some other issue, but none of the localhost API methods will call properly I always get the same response from it.


回答1:


I just ran into the same problem, it's a CORS issue. Just add in startup.cs of your API server something like this

app.UseCors(policy => 
    policy.WithOrigins("http://localhost:5000", "https://localhost:5001")
    .AllowAnyMethod()
    .WithHeaders(HeaderNames.ContentType));

Source: https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/blazor/common/samples/3.x/BlazorWebAssemblySample/Pages/CallWebAPI.razor



来源:https://stackoverflow.com/questions/63002320/blazor-webassembly-api-call-children-could-not-be-evaluated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!