CS1929 'IHttpClientFactory' 不包含 'GetFromJsonAsync' 的定义
CS1929 'IHttpClientFactory' does not contain a definition for 'GetFromJsonAsync'
我在 Blazor Server 项目的 Razor 组件页面 Index.razor 上收到消息“CS1929 - 'IHttpClientFactory' 不包含 'GetFromJsonAsync' 的定义”。我正在尝试发出 http 请求以将组件连接到数据库。
下面是index.razor中的代码:
@page "/people"
@inject IHttpClientFactory httpClientFactory
<h3>People</h3>
@if (people == null)
{<text>Loading...</text>}
else if (people.Length == 0)
{ <text>No people have been added to this database.</text>}
else
{
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Email ID</th>
<th>Address</th>
</tr>
</thead>
<tbody>
@foreach (var person in people)
{
<tr>
<td><a>Edit</a><button>Delete</button></td>
<td>@person.ID</td>
<td>@person.Title</td>
<td>@person.FirstName</td>
<td>@person.LastName</td>
<td>@person.PhoneNumber</td>
<td>@person.Email</td>
<td>@person.Address</td>
</tr>
}
</tbody>
</table>
}
@code {
Person[] people { get; set; }
protected override async Task OnInitializedAsync()
{
await LoadPeople();
}
async Task LoadPeople() => people = await httpClientFactory.GetFromJsonAsync<Person[]>("api/people");
}
然后我在 Startup.cs
中的 public void ConfigureServices(IServiceCollection services)
下添加了行 services.AddHttpClient();
错误在第 httpClientFactory
行 async Task LoadPeople() => people = await httpClientFactory.GetFromJsonAsync<Person[]>
我什至尝试用下面的替换它,但这只会导致更多错误,所以我删除了它:
var http = httpClientFactory.CreateClient();
async Task LoadPeople() => people = await http.GetFromJsonAsync<Person[]("api/people");
解决此问题的最佳方法是什么?每次我在尝试连接 http 时设法修复旧问题时都会遇到新问题。
您必须安装 System.Net.Http.Json 软件包...
Provides extension methods for System.Net.Http.HttpClient and System.Net.Http.HttpContent that perform automatic serialization and deserialization using System.Text.Json.
请在您的 nuget 控制台中执行此操作:
Install-Package System.Net.Http.Json -Version 3.2.1
代码示例:
public void ConfigureServices(IServiceCollection services)
{
// Chnage the url to yours
services.AddHttpClient("ServerAPI", client => client.BaseAddress =
new Uri("https://localhost:44371/"));
services.AddTransient(sp =>
sp.GetRequiredService<IHttpClientFactory>
().CreateClient("ServerAPI"));
}
并且在您的 FetchData 页面中:
@inject HttpClient httpClient
我在 Blazor Server 项目的 Razor 组件页面 Index.razor 上收到消息“CS1929 - 'IHttpClientFactory' 不包含 'GetFromJsonAsync' 的定义”。我正在尝试发出 http 请求以将组件连接到数据库。
下面是index.razor中的代码:
@page "/people"
@inject IHttpClientFactory httpClientFactory
<h3>People</h3>
@if (people == null)
{<text>Loading...</text>}
else if (people.Length == 0)
{ <text>No people have been added to this database.</text>}
else
{
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Email ID</th>
<th>Address</th>
</tr>
</thead>
<tbody>
@foreach (var person in people)
{
<tr>
<td><a>Edit</a><button>Delete</button></td>
<td>@person.ID</td>
<td>@person.Title</td>
<td>@person.FirstName</td>
<td>@person.LastName</td>
<td>@person.PhoneNumber</td>
<td>@person.Email</td>
<td>@person.Address</td>
</tr>
}
</tbody>
</table>
}
@code {
Person[] people { get; set; }
protected override async Task OnInitializedAsync()
{
await LoadPeople();
}
async Task LoadPeople() => people = await httpClientFactory.GetFromJsonAsync<Person[]>("api/people");
}
然后我在 Startup.cs
中的public void ConfigureServices(IServiceCollection services)
下添加了行 services.AddHttpClient();
错误在第 httpClientFactory
行 async Task LoadPeople() => people = await httpClientFactory.GetFromJsonAsync<Person[]>
我什至尝试用下面的替换它,但这只会导致更多错误,所以我删除了它:
var http = httpClientFactory.CreateClient();
async Task LoadPeople() => people = await http.GetFromJsonAsync<Person[]("api/people");
解决此问题的最佳方法是什么?每次我在尝试连接 http 时设法修复旧问题时都会遇到新问题。
您必须安装 System.Net.Http.Json 软件包...
Provides extension methods for System.Net.Http.HttpClient and System.Net.Http.HttpContent that perform automatic serialization and deserialization using System.Text.Json.
请在您的 nuget 控制台中执行此操作:
Install-Package System.Net.Http.Json -Version 3.2.1
代码示例:
public void ConfigureServices(IServiceCollection services)
{
// Chnage the url to yours
services.AddHttpClient("ServerAPI", client => client.BaseAddress =
new Uri("https://localhost:44371/"));
services.AddTransient(sp =>
sp.GetRequiredService<IHttpClientFactory>
().CreateClient("ServerAPI"));
}
并且在您的 FetchData 页面中:
@inject HttpClient httpClient