首页 > 解决方案 > 从 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 方法。

这个有什么替代品?

标签: asp.net-corehttpclientblazor-server-side

解决方案


不确定您的 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");
}

推荐阅读