asp.net-core - 从 Blazor 服务器调用 webapi 方法
问题描述
似乎这已被弃用,无法在 nuget 包中找到
Microsoft.AspNetCore.Blazor.HttpClient
public class EmployeeService : IEmployeeService
{
private readonly HttpClient httpClient;
public EmployeeService(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<IEnumerable<Employee>> GetEmployees()
{
return await httpClient.GetJsonAsync<Employee[]>("api/employees");
}
}
我正在尝试使用 httpClient 从 Blazor 调用 webapi 方法。
这个有什么替代品?
解决方案
不确定您的 taget 框架版本是什么。只需尝试添加System.Net.Http.Json
命名空间以检查它是否有效。
如果不工作,你可以GetFromJsonAsync
像下面这样使用:
using System.Net.Http.Json; //be sure add this namespace..
public async Task<IEnumerable<Employee>> GetEmployees()
{
return await httpClient.GetFromJsonAsync<Employee[]>("https://localhost:portNumber/api/employees");
}
推荐阅读
- html - 如何将不同的图片添加到共享同一类的一行 div 中?
- android - Dagger/MissingBinding androidx.lifecycle.ViewModelProvider.Factory 不能在没有@Provides-annotated 方法的情况下提供
- javascript - 在没有 I/O 的情况下,javascript(在浏览器中)中的异步/承诺是否有益?
- python - 使用 PyInstaller 编译 python,用 Python
- ionic3 - Ionic4 与 Ionic3 中的离子段
- .net - 如何在空 ADT 帐户中创建 Azure 数字孪生备份并将其还原?
- vhdl - 在包内使用但未声明的组件
- java - 如何在recyclerview 中为togglebutton.setOnCheckedChangedListener 创建一个接口?
- python - 如何返回满足条件的字典中的最后一个值
- ldap - WSO2am 与 ldap AD 集成时抛出“尝试连接到端点时出错。无法为 ssl://localhost:7712 借用客户端”