Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient.GetJsonAsync Not found. (blazor server)

I have installed the package by adding the latest package ref. from https://www.nuget.org/packages/Microsoft.AspNetCore.Blazor.HttpClient/ But still i am unable to find the desired function like .. Client.GetJsonAsync

Can you please help me out if i am missing something? Thanks.

I am trying out here but cant.

public async Task<User> GetUser(string Id)
        {
            HttpClient client = new HttpClient();
            var user = await client.GetJsonAsync($"{BaseUrl}Get-User/{Id}");
            return JsonConvert.DeserializeObject<User>(user);
        }
like image 374
sami ullah Avatar asked Jan 21 '20 18:01

sami ullah


2 Answers

1. Add nuget package System.Net.Http.Json

2. Add this namespace in using System.Net.Http.Json

Nothing worked for me and nobody pointing at this clearly.

These extension methods exists in System.Net.Http.Json namespace. To use these methods you need to first add this package System.Net.Http.Json. Then add this System.Net.Http.Json namespace in using. Only then json extension methods will appear with HttpClient object.

like image 90
Haider Ali Wajihi Avatar answered Nov 02 '22 23:11

Haider Ali Wajihi


The method signature is the following:

public static async Task<T> GetJsonAsync<T>(this HttpClient httpClient, string requestUri);

So it is a generic method and you will have to include the type argument in the call.

In your case, this should look like this:

HttpClient client = new HttpClient();
var user = await client.GetJsonAsync<User>($"{BaseUrl}Get-User/{Id}");

This will already deserialize the JSON response to the User type.

Note that you will need a using for the Microsoft.AspNetCore.Components namespace in order for this extension method to appear.

like image 23
poke Avatar answered Nov 03 '22 00:11

poke