Loading

HttpClientExtensions去了哪里

使用HttpClient实现http请求是非常常见的方式,有一个HttpClient的拓展类HttpClientExtensions提供了更多的拓展方法,包括但不限于

  • PostAsJsonAsync
  • PostAsXmlAsync
  • PutAsJsonAsync
  • PutAsXmlAsync

.Net 4.5之前HttpClientExtensions在System.Net.Http.dll,从.Net 4.5开始HttpClientExtensions在System.Net.Http.Formatting.dll
查证来源:HttpClientExtensions Methods (System.Net.Http) | Microsoft Docs

根据 json - HttpClient not supporting PostAsJsonAsync method C# - Stack OverflowCS1061 'HttpClient' does not contain a definition for 'PostAsJsonAsync' For asp.net core project · Issue #3849 · aspnet/AspNetCore.Docs可以得出以下3种解决办法。

  1. 根据PostAsync或者SendAsync实现
  2. 引用微软官方出的nuget包Microsoft.AspNet.WebApi.ClientNuGet Gallery | Microsoft.AspNet.WebApi.Client 5.2.7
  3. 引用他人出的nuget包

Microsoft.AspNet.WebApi.Client依赖

.NETFramework 4.5
Newtonsoft.Json (>= 6.0.4)
.NETStandard 2.0
Newtonsoft.Json (>= 10.0.1)
Newtonsoft.Json.Bson (>= 1.0.1)
Portable Class Library (.NETFramework 4.5, .NETCore 4.5, WindowsPhone 8.0, WindowsPhone 8.1, WindowsPhoneApp 8.1)
Microsoft.Net.Http (>= 2.2.22)
Newtonsoft.Json (>= 6.0.4)

posted @ 2019-09-11 17:21  repeatedly  阅读(749)  评论(0编辑  收藏  举报