您的位置:首页 > 编程语言 > ASP

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

2018-06-16 16:51 1046 查看

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();

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();
}
}

启动上面的 ASP.NET Core 站点,然后用 curl 命令发出请求

curl -X POST -d 'Hello World' localhost:5000

控制台就会输出期望的结果

Request.Body1:
Hello World
Request.Body2:
Hello World

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

EnableRewind 的实现源代码见 BufferingHelper.cs

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: