问题
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