Introduction
In this tutorial you’ll set up and deploy a production-ready ASP.NET Blazor Core application with a MongoDb Server on Ubuntu 18.04 using Nginx.
HttpClient not supporting PostAsJsonAsync method C#?
I am trying to call a web API from my web application. I am using .Net core and while writing the code I am getting the error HttpClient does not contain a definition PostAsJsonAsync method.
and I am getting the error message:
How it works
Remember the relationships.
OS/ ubuntu server 18.04-x64 Solution Folder
1.HttpClient only get string or stream from web api
2.System.Net.Http.Json
extension methods for HttpClient that allow serialization from/to JSON.
use httpclient get get string or stream from web api,then serialization class to json or deserialization json string to class
System.Net.Http.Json Provides extension methods for HttpClient, perform automatic serialization and deserialization using System.Text.Json.
3.System.Text.json
JsonSerializer used by System.Net.Http.Json.
ASP.NET Core Blazor 5.x(Server-side)-call Web API using System.Net.Http.Json+System.Text.json
Client side
Step 1:Reference the System.Net.Http.Json NuGet package in the project file
Step 2:Call Web API
Http.GetFromJsonAsync
PostAsJsonAsync
Call PostAsJsonAsync Sends an HTTP POST request
get ResponseMessage
read string content from ResponseMessage body
deserialize the string content(json format) to object
Step 1:Calls to PostAsJsonAsync
Step 2:get HttpResponseMessage
Calls to PostAsJsonAsync return an HttpResponseMessage
Step 3:get string from HttpResponseMessage body
Step 4:deserialize the JSON content from the response message
other:
you can Deserialize from content to object directly by ReadFromJsonAsync()
ReadFromJsonAsync come from System.Net.Http.Json,you can find these methods in NuGet package System.Net.Http.Json. But beware that it uses System.Text.Json internally.