• 00
  • :
  • 00
  • :
  • 00

ASP.NET Core 中读取 Request.Body 的正确姿势

ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream —— 不允许 Request.Body.Position=0 ,这就意味着只能读取一次,要想多次读取,需要借助 MemoryStream ,详见博问 asp.net core中2次读取Request.Body的问题 

using (var buffer = new MemoryStream())
{
    Request.Body.CopyTo(buffer);
    buffer.Position = 0;
    buffer.CopyTo(writer.BaseStream);
    Console.WriteLine("Request.Body:");
    buffer.Position = 0;
    buffer.CopyTo(Console.OpenStandardOutput());
} 

昨天读了博文 Reading request body in ASP.NET Core 之后得知在 ASP.NET Core 2.0 中已经针对这个问题提供了解决方法 —— EnableRewind() ,只要启用倒带功能,就可以让  Request.Body 回归正常 Stream 。

使用非常简单,引用命名空间 Microsoft.AspNetCore.Http.Internal ,调用方法 Request.EnableRewind()  即可,下面我们用简单的示例代码体验一下

public class HomeController : Controller
{
    public IActionResult Index()
    {
      
Request.EnableRewind(); // .net core 3.0为 Request.EnableBuffering (); Console.WriteLine(
"Request.Body1:"); Request.Body.Position = 0; Request.Body.CopyTo(Console.OpenStandardOutput()); Console.WriteLine(); Console.WriteLine("Request.Body2:"); Request.Body.Position = 0; Request.Body.CopyTo(Console.OpenStandardOutput()); Console.WriteLine(); return Ok(); } }

EnableRewind 有 2 个参数 bufferThreshold 与 bufferLimit 。 bufferThreshold 设置的是 Request.Body 最大缓存字节数(默认是30K),超出这个阈值的字节会被写入磁盘;bufferLimit 设置的是 Request.Body 允许的最大字节数(默认值是null),超出这个限制,就会抛出异常  System.IO.IOException 。

EnableRewind 的实现源代码见 BufferingHelper.cs

更新:.NET Core 3.0 中 EnableRewind 被改名为 EnableBuffering 。

posted @ 2020-07-31 11:30  Garson_Zhang  阅读(347)  评论(0编辑  收藏  举报