每日踩坑 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         }


转载请标明出处

作者:AaXuan

地址:http://www.cnblogs.com/Aaxuan

知识共享许可协议

本作品采用  知识共享署名 3.0 未本地化版本许可协议  进行许可。

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