[英]Where is HttpContent.ReadAsAsync?
I see in tons of examples on the web using the new HttpClient
object (as part of the new Web API) that there should be HttpContent.ReadAsAsync<T>
method.我在网上看到大量使用新
HttpClient
对象(作为新 Web API 的一部分)的示例,应该有HttpContent.ReadAsAsync<T>
方法。 However, MSDN doesn't mention this method, nor does IntelliSense find it.但是, MSDN没有提到这种方法,IntelliSense 也没有找到。
Where did it go, and how do I work around it?它去了哪里,我该如何解决?
It looks like it is an extension method (in System.Net.Http.Formatting):看起来它是一个扩展方法(在 System.Net.Http.Formatting 中):
HttpContentExtensions Class HttpContentExtensions 类
Update:更新:
PM> install-package Microsoft.AspNet.WebApi.Client
PM> 安装包 Microsoft.AspNet.WebApi.Client
According to the System.Net.Http.Formatting NuGet package page, the System.Net.Http.Formatting
package is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Client
package available on NuGet here .根据System.Net.Http.Formatting NuGet 包页面,
System.Net.Http.Formatting
包现在是旧包,可以在NuGet 此处提供的Microsoft.AspNet.WebApi.Client
包中找到。
I have the same problem, so I simply get JSON string and deserialize to my class:我有同样的问题,所以我只是获取 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);
If you are already using Newtonsoft.Json
and don't want to install Microsoft.AspNet.WebApi.Client
:如果您已经在使用
Newtonsoft.Json
并且不想安装Microsoft.AspNet.WebApi.Client
:
var myInstance = JsonConvert.DeserializeObject<MyClass>(
await response.Content.ReadAsStringAsync());
You can write extention method:您可以编写扩展方法:
public static async Task<Tout> ReadAsAsync<Tout>(this System.Net.Http.HttpContent content) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<Tout>(await content.ReadAsStringAsync());
}
只需右键单击您的项目,转到管理 NuGet 包搜索 Microsoft.AspNet.WebApi.Client 安装它,您将可以访问扩展方法。
2021 Update: Looks like the method is removed in .NET5. 2021 年更新:看起来该方法已在 .NET5 中删除。 Alternatively, you can use
ReadFromJsonAsync<>()
from System.Net.Http.Json.HttpContentJsonExtensions
.或者,您可以使用
System.Net.Http.Json.HttpContentJsonExtensions
ReadFromJsonAsync<>()
。 It solves the purpose.它解决了目的。
Having hit this one a few times and followed a bunch of suggestions, if you don't find it available after installing the NuGet Microsoft.AspNet.WebApi.Client manually add a reference from the packages folder in the solution to:已经点击了几次并遵循了一系列建议,如果您在安装 NuGet Microsoft.AspNet.WebApi.Client 后没有发现它可用,则手动从解决方案中的包文件夹中添加一个引用到:
\Microsoft.AspNet.WebApi.Client.5.2.6\lib\net45\System.Net.Http.Formatting.dll
And don't get into the trap of adding older references to the System.Net.Http.Formatting.dll NuGet并且不要陷入向 System.Net.Http.Formatting.dll NuGet 添加旧引用的陷阱
Although I had the same problem, the answers in this thread didn't fully help me to fix the problem.尽管我遇到了同样的问题,但该线程中的答案并没有完全帮助我解决问题。 For this reason, I decided to write the result of my research in this post.
出于这个原因,我决定在这篇文章中写下我的研究结果。 To fix this issue, follow the steps below:
要解决此问题,请按照以下步骤操作:
Tools > NuGet Package Manager > Package Manager Console
in Visual Studio IDE and add the Microsoft.AspNet.WebApi.Client
package to the solution.Tools > NuGet Package Manager > Package Manager Console
打开包管理器控制台,并将Microsoft.AspNet.WebApi.Client
包添加到解决方案。Install-Package Microsoft.AspNet.WebApi.Client -Version 5.2.7
System.Net.Http.Formatting.dll
file should be present in the directory shown below as a result of the first step.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.
右键单击解决方案中 ASP.NET 项目中的“引用”部分,单击“添加引用...”部分。 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.由于文件
System.Net.Http.Formatting.dll
是扩展名,因此在直接搜索时不会像Microsoft.AspNet.WebApi.Client
包中的其他项一样列出它。 Therefore, to add the DLL file manually, click the "Browse..." button at the bottom of the "Reference Manager" window.因此,要手动添加 DLL 文件,请单击“参考管理器”窗口底部的“浏览...”按钮。 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.选择第二步所示目录中的
System.Net.Http.Formatting.dll
文件,并选中复选框以将 DLL 文件包含在项目中。
Include the System.Net.Http
namespace in the project to use the features provided by this DLL in the project;在项目中包含
System.Net.Http
命名空间,以在项目中使用此 DLL 提供的功能; using System.Net.Http.Formatting;
declaration is available within the HttpContentExtensions
static class.声明在
HttpContentExtensions
静态类中可用。
using System.Net.Http;
OPTIONAL: You can achieve a similar solution by installing one of the System.Net.Http.Formatting.Extension or WebApiDoodle.Net.Http.Formatting packages and following the steps above.可选:您可以通过安装System.Net.Http.Formatting.Extension或WebApiDoodle.Net.Http.Formatting包之一并执行上述步骤来实现类似的解决方案。
Install-Package System.Net.Http.Formatting.Extension -Version 5.2.3
Install-Package WebApiDoodle.Net.Http.Formatting -Version 3.0.0-pre01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.