首页 新闻 赞助 找找看

在.net core 里面,哪个包能代替 System.Net.Http.Formatting

0
[已关闭问题] 关闭于 2016-08-01 11:51

在 full .net framework中,可以使用该类库中的扩展方法,将HttpContent强类型化输出。

https://www.nuget.org/packages/System.Net.Http.Formatting.Extension/

 

但是不支持  netstandard1.x

虾。的主页 虾。 | 菜鸟二级 | 园豆:257
提问于:2016-07-26 15:47
< >
分享
所有回答(1)
1

自己实现了个简单的

        public static async Task<T> ReadAsync<T>(this HttpContent httpContent)
        {
            var mediaType = httpContent.Headers.ContentType.MediaType;
            if (mediaType != "application/json")
            {
                throw new FormatException();
            }
            using (var stream = await httpContent.ReadAsStreamAsync())
            using (var sr = new StreamReader(stream))
            using (var jsonTestReader = new JsonTextReader(sr))
            {
                var jsonSerializer = new JsonSerializer();
                return jsonSerializer.Deserialize<T>(jsonTestReader);
            }
        }

 

 

 

虾。 | 园豆:257 (菜鸟二级) | 2016-07-26 17:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册