Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'HttpClient' does not contain a definition for 'GetFromJsonAsync'

Tags:

c#

.net

blazor

I created a new Blazor app and I want to access a URL through HttpClient I faced this error

'HttpClient' does not contain a definition for 'GetFromJsonAsync' and no accessible extension method 'GetFromJsonAsync' accepting a first argument of type 'HttpClient' could be found (are you missing a using directive or an assembly reference?) [BlazorApp]csharp(CS1061) ,

this is my code

 @inject HttpClient Http
    <h1>@name</h1>
    @code {
        private string name;
    
        protected override async Task OnInitializedAsync()
        {
            name = await Http.GetFromJsonAsync<string>("api/");
        }
    }
like image 844
esamaldin elzain Avatar asked Jan 24 '23 17:01

esamaldin elzain


1 Answers

You need to add the following using directive:

using System.Net.Http.Json;

GetFromJsonAsync is an extension method, not part of HttpClient itself.

like image 128
Johnathan Barclay Avatar answered Jan 29 '23 08:01

Johnathan Barclay