受欢迎的博客标签

ASP.NET Core Blazor 5.x(Server-side)-call Web API using System.Net.Http.Json+System.Text.json

Published

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:

Error: 'System.Net.Http.HttpClient' does not contain a definition for 'PostAsJsonAsync' and No extension method 'PostAsJsonAsync' accepting a first argument of type 'System.Net.Http.HttpClient' could be found (are you missing a using directive or an assembly reference?)

How it works

Remember the relationships.

OS/ ubuntu server 18.04-x64 Solution Folder

Call Web Api ---------- root
 ├── HttpClient(System.Net.Http)       ----------HttpClient
 │   ├── GetAsync(string? requestUri); ----------Task<HttpResponseMessage> GetAsync(string? requestUri);
 │   ├──  PutAsync(Uri? requestUri, HttpContent content); ---------- Task<HttpResponseMessage> PutAsync(Uri? requestUri, HttpContent content);
 │   ├──  PatchAsync ---------- public Task<HttpResponseMessage> PatchAsync(string? requestUri, HttpContent content);
 │   └──  DeleteAsync ---------- Task<HttpResponseMessage> DeleteAsync(Uri? requestUri);
 ├── HttpClient Extend(System.Net.Http.Json)<---------- HttpClient Json Extensions, it uses System.Text.Json internally
 │   ├── GetFromJsonAsync ---------- HttpClient Json Extensions
 │   ├── PutAsJsonAsync ---------- HttpClient Json Extensions
 │   └── PostAsJsonAsync ---------- 
 ├── JsonSerializer(System.Text.json)   ---------- JSON helpers,used by System.Net.Http.Json
 │   ├── Deserialize ---------- 
 │   └── Serialize ---------- 
 └── end ---------- 

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

PM> Install-Package System.Net.Http     //for HttpClient   HttpClient httpClient = new HttpClient();
PM> Install-Package System.Net.Http.Json  //for httpClient.PutAsJsonAsync
PM> Install-Package System.Text.json     //for System.Text.Json.JsonSerializer.Deserialize<T>(jsonstr);

Step 2:Call Web API

Http.GetFromJsonAsync

todoItems = await Http.GetFromJsonAsync<TodoItem[]>("api/TodoItems");

 

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
var addItem = new TodoItem { Name = newItemName, IsComplete = false };
        await Http.PostAsJsonAsync("api/TodoItems", addItem);
Step 2:get  HttpResponseMessage

Calls to PostAsJsonAsync return an HttpResponseMessage

 HttpResponseMessage response = await Http.PostAsJsonAsync<MultiFilterSearchModel>(GlobalHelper.BaseUrl + "/api/v1/StockDataPhysicalDiskMultiFilterPankou/SearchAsync", searchModel);
Step 3:get string from HttpResponseMessage body
 string jsonResponse = await response.Content.ReadAsStringAsync();

 

Step 4:deserialize the JSON content from the response message
forecasts = System.Text.Json.JsonSerializer.Deserialize<Stockso.Models.ApiResponse<SearchResultStockModel>>(jsonResponse);

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.

 //or provider:System.Net.Http.Json
            forecasts = await response.Content.ReadFromJsonAsync<Stockso.Models.ApiResponse<SearchResultStockModel>>();