Where is the PostAsJsonAsync method in ASP.NET Core?

后端 未结 14 1751
再見小時候
再見小時候 2020-12-29 00:51

I was looking for the PostAsJsonAsync() extension method in ASP.NET Core. Based on this article, it\'s available in the Microsoft.AspNet.WebApi.Client

14条回答
  •  孤城傲影
    2020-12-29 01:41

    That is not part of the ASP.NET Core project. However you can proceed with:

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://myurl/api");
    
    string json = JsonConvert.SerializeObject(myObj);
    
    request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
    
    HttpClient http = new HttpClient();
    HttpResponseMessage response = await http.SendAsync(request);
    
    if (response.IsSuccessStatusCode)
        {                
        }
    else
        {
        }
    

提交回复
热议问题