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

测试代码:

 

结果:

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         }

猜你喜欢

转载自www.cnblogs.com/Aaxuan/p/9725232.html