这是我第一次跳转到C#、Blazor和razor页面,我正在尝试页面,该页面将以用户可读的形式显示请求的JSON信息。然而,当我运行这段代码时,我遇到了一个错误,说它无法转换?
public interface IJokeService
{
Task<IEnumerable<Joke>> GetJokes();
}
public class JokeService : IJokeService
{
private readonly HttpClient httpClient;
public JokeService (HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<IEnumerable<Joke>> GetJokes()
{
return await httpClient.GetFromJsonAsync<Joke[]>("/random_joke");
}
}
@page "/fetchjokes"
@using GettingStarted.Shared
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using GettingStarted.Client.Services
@attribute [Authorize]
<h3>FetchJokes</h3>
@if (Jokec == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>General</th>
<th>Setup</th>
<th>punchline</th>
</tr>
</thead>
<tbody>
@foreach (var Jokes in Jokec)
{
<tr>
<td>@Jokes.id</td>
<td>@Jokes.type</td>
<td>@Jokes.setup</td>
<td>@Jokes.punchline</td>
</tr>
}
</tbody>
</table>
}
@inject IJokeService jokeService
@code
{
public IEnumerable<Joke> Jokec;
protected override async Task OnInitializedAsync()
{
Jokec = (await jokeService.GetJokes()).ToList();
}
}
如果有人能帮助我揭露我做错了什么,或者用更好的方法来启发我,我将不胜感激
我试图接收的JSONhttps://official-joke-api.appspot.com/random_joke
链接产生一个单个笑话。
你告诉亵渎者期待一系列的笑话。
public async Task<Joke> GetJokes()
{
return await httpClient.GetFromJsonAsync<Joke>("/random_joke");
}