ASP.NET Core 2.0 中读取 Request.Body 的正确姿势
原文: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 = ;
buffer.CopyTo(writer.BaseStream);
Console.WriteLine("Request.Body:");
buffer.Position = ;
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 = ;
Request.Body.CopyTo(Console.OpenStandardOutput());
Console.WriteLine(); Console.WriteLine("Request.Body2:");
Request.Body.Position = ;
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
更新:.NET Core 3.0 中 EnableRewind 被改名为 EnableBuffering 。
ASP.NET Core 2.0 中读取 Request.Body 的正确姿势的更多相关文章
- ASP.NET Core 5.0 中读取Request中Body信息
ASP.NET Core 5.0 中读取Request中Body信息 记录一下如何读取Request中Body信息 public class ValuesController : Controller ...
- 如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?
原文:如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容? 文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和 ...
- ASP.NET Core 中读取 Request.Body 的正确姿势
ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream —— 不允许 Request.Body.Position=0 ,这就意味着只能 ...
- ASP.NET Core 1.0 中使用 Swagger 生成文档
github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...
- 如何在ASP.NET Core 2.0中使用Razor页面
如何在ASP.NET Core 2.0中使用Razor页面 DotNetCore2017-11-22 14:49 问题 如何在ASP.NET Core 2.0中使用Razor页面 解 创建一个空的项 ...
- 探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs
原文:探索ASP.Net Core 3.0系列二:聊聊ASP.Net Core 3.0 中的Startup.cs 前言:.NET Core 3.0 SDK包含比以前版本更多的现成模板. 在本文中,我将 ...
- 【转】ASP.NET Core 2.0中的HttpContext
ASP.NET Core 2.0中的HttpContext相较于ASP.NET Framework有一些变化,这边列出一些之间的区别. 在ASP.NET Framework中的 System. ...
- ASP.NET Core 1.0 中的依赖项管理
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- 在ASP.NET Core 1.0中如何发送邮件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...
随机推荐
- es搭建过程会存在的问题 针对6.x
常见的四个基本错误 错误1 can not run elasticsearch as root 解决方案: 因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户 第一步 ...
- apt如何列出所有已经安装的软件包
apt如何列出所有已经安装的软件包 转 https://www.helplib.com/ubuntu/article_155294 问题: 我想将所有已安装软件包的列表输出到文本文件中,以便我可以查看 ...
- python 学生表
1,主页面函数(01-mainpage.py) import json import file_manager import student_system ''' ''' # 全局变量 file_na ...
- Tomcat下配置javaWeb访问路径-Linux
首先找到你的Tomcat的server.xml文件 笔者直接编辑 sudo vi /app/prod/conf/server.xml 访问只需要您的IP地址就可以访问项目了 第二个配置则是访问 ip+ ...
- eclipse、MyEclipse 修改字符集和JDK
eclipse 中UTF-8设置 1.windows->Preferences 打开"首选项"对话框: 2.然后,general->Workspace,右 侧Tex ...
- MySQL之LEFT JOIN中使用ON和WHRERE对表数据
背景 left join在我们使用mysql查询的过程中可谓非常常见,比如博客里一篇文章有多少条评论.商城里一个货物有多少评论.一条评论有多少个赞等等.但是由于对join.on.where等关键字的不 ...
- 配置zabbix监控windows,cmd运行报错cannot connect to Service Manager: [0x00000005]
错误原因: cmd运行没有管理员权限 解决: 找到cmd.exe的位置C:\Windows\System32,选中--右键--使用管理员身份运行 再通过cmd进入到zabbix_agentd文件夹进行 ...
- MFC中使用ADO进行数据库操作
参考FROM:http://hi.baidu.com/sunkanghome/item/e1fda510b3186359f1090ee2 数据库与数据库编程: 当前各种主流数据库有很多,包括Oracl ...
- idea启动不了,报错Address localhost:1099 is already in use
解决方法 win + R 然后输入cmd netstat -ano|findstr 1099 taskkill -f -pid 你的PID
- CentOS7.3安装Python3.6
安装python3.6可能使用的依赖 # yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sql ...