我在网络上看到大量使用新的HttpClient对象(作为新web API的一部分)的例子,应该有HttpContent。ReadAsAsync < T >方法。但是,MSDN没有提到这个方法,智能感知也没有找到它。
它去哪里了,我该如何解决它?
我在网络上看到大量使用新的HttpClient对象(作为新web API的一部分)的例子,应该有HttpContent。ReadAsAsync < T >方法。但是,MSDN没有提到这个方法,智能感知也没有找到它。
它去哪里了,我该如何解决它?
当前回答
虽然我也有同样的问题,但这个帖子中的答案并没有完全帮助我解决问题。因此,我决定把我的研究结果写在这篇文章里。要修复此问题,请执行以下步骤:
使用NuGet将Microsoft.AspNet.WebApi.Client包添加到项目中。而在ASP。在Visual Studio IDE的Tools > NuGet Package Manager > Package Manager Console中打开包管理器控制台,并将Microsoft.AspNet.WebApi.Client包添加到解决方案中。
Install-Package Microsoft.AspNet.WebApi.Client -Version 5.2.7
安装后,检查系统上是否存在扩展DLL。作为第一步的结果,System.Net.Http.Formatting.dll文件应该出现在如下所示的目录中。
{root-solution-directory}\packages\Microsoft.AspNet.WebApi.Client.{package-version}\lib\net45\
Manually add the reference to the relevant project. Right click on the "References" section in the ASP.NET project within the solution click on the "Add Reference..." section. Since the file System.Net.Http.Formatting.dll is an extension, it will not be listed when searched directly like other items in the Microsoft.AspNet.WebApi.Client package. Therefore, to add the DLL file manually, click the "Browse..." button at the bottom of the "Reference Manager" window. Select the System.Net.Http.Formatting.dll file in the directory shown in the second step and check the checkbox to include the DLL file in the project. Include the System.Net.Http namespace in the project to use the features provided by this DLL in the project; using System.Net.Http.Formatting; declaration is available within the HttpContentExtensions static class.
using System.Net.Http;
可选:您可以通过安装System.Net.Http.Formatting.Extension或WebApiDoodle.Net.Http.Formatting包并遵循上面的步骤来实现类似的解决方案。
其他回答
虽然我也有同样的问题,但这个帖子中的答案并没有完全帮助我解决问题。因此,我决定把我的研究结果写在这篇文章里。要修复此问题,请执行以下步骤:
使用NuGet将Microsoft.AspNet.WebApi.Client包添加到项目中。而在ASP。在Visual Studio IDE的Tools > NuGet Package Manager > Package Manager Console中打开包管理器控制台,并将Microsoft.AspNet.WebApi.Client包添加到解决方案中。
Install-Package Microsoft.AspNet.WebApi.Client -Version 5.2.7
安装后,检查系统上是否存在扩展DLL。作为第一步的结果,System.Net.Http.Formatting.dll文件应该出现在如下所示的目录中。
{root-solution-directory}\packages\Microsoft.AspNet.WebApi.Client.{package-version}\lib\net45\
Manually add the reference to the relevant project. Right click on the "References" section in the ASP.NET project within the solution click on the "Add Reference..." section. Since the file System.Net.Http.Formatting.dll is an extension, it will not be listed when searched directly like other items in the Microsoft.AspNet.WebApi.Client package. Therefore, to add the DLL file manually, click the "Browse..." button at the bottom of the "Reference Manager" window. Select the System.Net.Http.Formatting.dll file in the directory shown in the second step and check the checkbox to include the DLL file in the project. Include the System.Net.Http namespace in the project to use the features provided by this DLL in the project; using System.Net.Http.Formatting; declaration is available within the HttpContentExtensions static class.
using System.Net.Http;
可选:您可以通过安装System.Net.Http.Formatting.Extension或WebApiDoodle.Net.Http.Formatting包并遵循上面的步骤来实现类似的解决方案。
2021更新:看起来在。net 5中删除了该方法。或者,您可以使用ReadFromJsonAsync<>()从System.Net.Http.Json.HttpContentJsonExtensions。它解决了问题。
我有同样的问题,所以我只是得到JSON字符串和反序列化到我的类:
HttpResponseMessage response = await client.GetAsync("Products");
//get data as Json string
string data = await response.Content.ReadAsStringAsync();
//use JavaScriptSerializer from System.Web.Script.Serialization
JavaScriptSerializer JSserializer = new JavaScriptSerializer();
//deserialize to your class
products = JSserializer.Deserialize<List<Product>>(data);
它看起来像是一个扩展方法(在System.Net.Http.Formatting中):
HttpContentExtensions课
更新:
PM>安装包Microsoft.AspNet.WebApi.Client
根据System.Net.Http.Formatting NuGet包页面,System.Net.Http.Formatting包现在是遗留的,可以在NuGet上的Microsoft.AspNet.WebApi.Client包中找到。
如果你已经在使用Newtonsoft。Json,不想安装Microsoft.AspNet.WebApi.Client:
var myInstance = JsonConvert.DeserializeObject<MyClass>(
await response.Content.ReadAsStringAsync());