码迷,mamicode.com
首页 > Web开发 > 详细

每日踩坑 2018-09-29 .Net Core 控制器中读取 Request.Body

时间:2018-11-06 00:53:52      阅读:589      评论:0      收藏:0      [点我收藏+]

标签:odi   src   控制器   enable   ade   http   position   copyto   ble   

测试代码:

 技术分享图片

 

结果:

 

技术分享图片

 

PostMan:

技术分享图片

 

 

代码:

 1         private string GetRequestBodyUTF8String()
 2         {
 3             this.Request.EnableBuffering();
 4             this.Request.Body.Position = 0;
 5             Encoding encoding = System.Text.UTF8Encoding.Default;
 6             if (this.Request.ContentLength > 0 && this.Request.Body != null && this.Request.Body.CanRead)
 7             {
 8                 using (var buffer = new MemoryStream())
 9                 {
10                     this.Request.Body.CopyTo(buffer);
11                     buffer.Position = 0;
12                     var reader = new StreamReader(buffer, encoding);
13                     var body = reader.ReadToEnd();
14                     return body;
15                 }
16             }
17 
18             return string.Empty;
19         }

 

每日踩坑 2018-09-29 .Net Core 控制器中读取 Request.Body

标签:odi   src   控制器   enable   ade   http   position   copyto   ble   

原文地址:https://www.cnblogs.com/Aaxuan/p/9725232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!