Baba
Baba

Reputation: 2219

Calling a webapi method from Blazor server

It seems like this is deprecated and cannot be found in nuget package

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");
                }
            }

I am trying to call a webapi method from Blazor using httpClient.

What is the substitute for this?

Upvotes: 0

Views: 640

Answers (2)

dmc2005
dmc2005

Reputation: 595

The tutorial many people have sourced their code from has a mistype - should be:

return await httpClient.GetJsonFromAsync<Employee[]>("api/employees");

Upvotes: 0

Rena
Rena

Reputation: 36645

Not sure what is your taget framework version. Just try to add System.Net.Http.Json namespace to check if it works or not.

If not working, you can use GetFromJsonAsync like below:

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");
}

Upvotes: 1

Related Questions