Hi, I am using Blazor Server version which is shipped with ASP.NET Core 3.0. I could not find the "GetJsonAsync" method inside HttpClient class. Is there any work around?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Probably missing the package in your project file ...
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.0.0" />
@guardrex We should make that package requirement more visible in the doc. I also missed that detail when I was trying to do this.
I'll go with a normal "Package" section like we often do.
@guardrex I can get all other methods like GetAsync, PostAsync from same HttpClient. Only GetJsonAsync and PostJsonAsyc and other Json methods missing. Didn't you include in json serialization in Blazor server?
I have created a CRUD blazor server app with manually convert to StringContent (using Newtonsoft) in PostAsync and also converted from HttpResponse to object using same Newtonsoft Deserializer.
Sorry .... sorry ... you've caught me at the very end of the day. I'm brain-fried for the day ... been up since 5am! Yikes!.
These bits only work for Blazor WebAssembly. For Blazor Server, engineering says to use IHttpClientFactory, and I think those bits use System.Text.Json
and there's a JsonSerializer
. Check that doc ... I think it explains it all out.
I have just checked "Microsoft.AspNetCore.Blazor.HttpClient" and found that this is still an experimental and preview only. Is that correct?
Leave this issue open tho ...... I _do_ want to add a section for the package here. This issue will automatically close when the PR merges.
Yes, that's correct. All of Blazor WebAssembly is in preview, including that package.
@sarathlalsaseendran You'll need to install the following package to gain access to methods like PostAsJsonAsync
:
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
In 3.0, Json.NET (Newtonsoft) was removed from the shared framework. As a result, the Web API client package was removed too. It had a dependency on Json.NET. The default JSON serializer in 3.0 is System.Text.Json.
Thanks @scottaddie ... we have the cross-link in this Blazor topic; but when I go to add the Package section, I'll double-check and work on the visibility of the text/link for Blazor server. Tomorrow tho! I'm 😵 at this point.
Hi @scottaddie, Thanks for the suggestion. I have added the "Microsoft.AspNet.WebApi.Client" package as per your suggestion. But still not getting GetJsonAsync method in HttpCleint. I am getting "PostAsAsync" and "PutAsAsycn" methods. Can you please tell me is there any work around to get GetAsAsync method in HttpClient.
Did you mean ReadAsAsync
? That should be on the response content.
Hi @pranavkm, I am asking about "GetAsJsonAsync" method in HttpClient class. In previous versions of Blazor (prior to .NET Core 3.0) it was there. Now after I added "Microsoft.AspNet.WebApi.Client" package (as per @scottaddie ) I could see "PostAsAsync" and "PutAsAsync" methods. Do you have any update?
GetAsJsonAsync
is an API specific to the experimental package. You can use HttpContent.ReadAsAsync<T>
as a substitute: https://stackoverflow.com/a/24052147.
Most helpful comment
Hi @scottaddie, Thanks for the suggestion. I have added the "Microsoft.AspNet.WebApi.Client" package as per your suggestion. But still not getting GetJsonAsync method in HttpCleint. I am getting "PostAsAsync" and "PutAsAsycn" methods. Can you please tell me is there any work around to get GetAsAsync method in HttpClient.