C# HttpContent.ReadAsAsync 在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10399324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is HttpContent.ReadAsAsync?
提问by David Pfeffer
I see in tons of examples on the web using the new HttpClientobject (as part of the new Web API) that there should be HttpContent.ReadAsAsync<T>method. However, MSDNdoesn't mention this method, nor does IntelliSense find it.
我在网络上使用新HttpClient对象(作为新 Web API 的一部分)的大量示例中看到应该有HttpContent.ReadAsAsync<T>方法。但是,MSDN没有提到这种方法,IntelliSense 也没有找到。
Where did it go, and how do I work around it?
它去了哪里,我该如何解决?
采纳答案by J...
It looks like it is an extension method (in System.Net.Http.Formatting):
看起来它是一个扩展方法(在 System.Net.Http.Formatting 中):
Update:
更新:
PM> install-package Microsoft.AspNet.WebApi.Client
PM> 安装包 Microsoft.AspNet.WebApi.Client
According to the System.Net.Http.FormattingNuGet package page, the System.Net.Http.Formattingpackage is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Clientpackage available on NuGet here.
根据System.Net.Http.FormattingNuGet 包页面,该System.Net.Http.Formatting包现在是旧Microsoft.AspNet.WebApi.Client包,可以在此处的 NuGet 上提供的包中找到。
回答by rosta
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);
回答by Ivan Carmenates García
Just right click in your project go Manage NuGet Packages search for Microsoft.AspNet.WebApi.Client install it and you will have access to the extension method.
只需右键单击您的项目,转到管理 NuGet 包搜索 Microsoft.AspNet.WebApi.Client 安装它,您将可以访问扩展方法。
回答by Martin Brandl
If you are already using Newtonsoft.Jsonand don't want to install Microsoft.AspNet.WebApi.Client:
如果您已经在使用Newtonsoft.Json并且不想安装Microsoft.AspNet.WebApi.Client:
var myInstance = JsonConvert.DeserializeObject<MyClass>(
await response.Content.ReadAsStringAsync());
回答by Tom John
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 添加旧引用的陷阱
回答by Vasya Milovidov
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());
}

