0

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?

2 Answers 2

1

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");
}
2
  • I am using .net framework 5
    – Baba
    Oct 16, 2021 at 3:28
  • No, it is impossible with .net framework if you use blazor. It must be .net core. I think you may use .Net 5. So do you check my shared code? It should be work because I use .Net 5. Please copy my code to your project.
    – Rena
    Oct 18, 2021 at 1:22
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.