2019-9-19-dotnet-找不到-PostAsJsonAsync-方法
title | author | date | CreateTime | categories |
---|---|---|---|---|
dotnet 找不到 PostAsJsonAsync 方法 |
lindexi |
2019-09-19 14:53:58 +0800 |
2019-09-19 14:53:57 +0800 |
dotnet |
在网络编程可以使用 PostAsJsonAsync 这个方便的方法将一个对象作为 json 推送到服务器,这个方法是一个扩展方法,在之前的框架,可以在 System.Net.Http.dll 找到这个好用的扩展方法,但是在 4.5 的时候就被移除了。本文告诉大家如何安装这个扩展方法
在新的版本,可以通过安装 Microsoft.AspNet.WebApi.Client 这个库使用,支持 .NETFramework 4.5 和 .NETStandard 2.0 另外不要看这个库的命名空间包含了 AspNet 其实这个库就是一些扩展方法,没有引用任何 AspNet 代码
在使用的时候,需要引用 System.Net.Http
命名空间,然后就可以使用下面的代码
await httpClient.PostAsJsonAsync(url, obj);
其实自己写一个扩展方法也是不错,例如我就需要通过 Get 一个对象,可以这样写
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
public static class HttpClientExtensions
{
public static async Task<T> GetObjectAsync<T>(this HttpClient httpClient, string url)
{
var str = await httpClient.GetStringAsync(url);
return JsonConvert.DeserializeObject<T>(str);
}
}
通过自己定义扩展方法,可以让网络编程的时候减少重复代码
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容
![知识共享许可协议](https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png)
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。