使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流
原文:使用Enablebuffering多次读取Asp Net Core 请求体
使用Enablebuffering多次读取Asp Net Core 请求体
1 .Net Core 2.X时代
使用EnableRewind倒带
public IActionResult Index()
{
Request.EnableRewind();
using (var reader = new StreamReader(Request.Body))
{
var body = reader.ReadToEnd();
// Do something
Request.Body.Seek(0, SeekOrigin.Begin);
body = reader.ReadToEnd();
}
// More code
return View("Index");
}
2 .NET Core 3.0时代
因为.NET Core 3.0 preview 6以后(6还是可以使用的), Microsoft.AspNetCore.Http.Internal不再是公有方法. 所以EnableRewind不能使用。
使用Enablebuffering
public async Task<IActionResult> Index()
{
Request.EnableBuffering();
using (var reader = new StreamReader(Request.Body, encoding: Encoding.UTF8))
{
var body = reader.ReadToEndAsync();
// Do some processing with body…
// Reset the request body stream position so the next middleware can read it
Request.Body.Position = 0;
}
return View();
}
// 默认不支持同步方法:ReadToEnd()
// 需要设置AllowSynchronousIO=true;
// System.InvalidOperationException: 'Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.'
如:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel((context, options) =>
{
options.AllowSynchronousIO = true;
//设置应用服务器Kestrel请求体最大为50MB
options.Limits.MaxRequestBodySize = ;
});
webBuilder.UseStartup<Startup>();
});
3. 参考
使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流的更多相关文章
- ASP.Net Core 5.0 MVC 配置文件读取,Startup 类中ConfigureServices 方法、Configure 方法的使用
配置文件读取 1. 新建FirstController控制器 在appsettings文件内容替换成以下代码 { "Position": { "Title": ...
- asp.net core 2.0类库项目读取配置文件
1.首先在类库项目中添加 这3个库. 2.在类库项目中添加AppSetting.cs.代码如下: using Microsoft.Extensions.Configuration;using Syst ...
- 使用Enablebuffering多次读取Asp Net Core 请求体
使用Enablebuffering多次读取Asp Net Core 请求体 1 .Net Core 2.X时代 使用EnableRewind倒带 public IActionResult Index( ...
- ASP.NET Core 2.0 中读取 Request.Body 的正确姿势
原文:ASP.NET Core 中读取 Request.Body 的正确姿势 ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream ...
- ASP.NET Core 5.0 中读取Request中Body信息
ASP.NET Core 5.0 中读取Request中Body信息 记录一下如何读取Request中Body信息 public class ValuesController : Controller ...
- 初识ASP.NET Core 1.0
本文将对微软下一代ASP.NET框架做个概括性介绍,方便大家进一步熟悉该框架. 在介绍ASP.NET Core 1.0之前有必要澄清一些产品名称及版本号.ASP.NET Core1.0是微软下一代AS ...
- ASP.NET Core 1.0 静态文件、路由、自定义中间件、身份验证简介
概述 ASP.NET Core 1.0是ASP.NET的一个重要的重新设计. 例如,在ASP.NET Core中,使用Middleware编写请求管道. ASP.NET Core中间件对HttpCon ...
- 跨平台运行ASP.NET Core 1.0
前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为 ...
- ASP.NET Core 1.0开发Web API程序
.NET Core版本:1.0.0-rc2Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2开发及运行平台:Windows ...
随机推荐
- mysql数据库的还原及常见问题解决
例如:需要还原的数据库脚本文件为test.sql,脚本中已包含数据库的创建,test.sql所在目录为/home 1.常用source命令 进入mysql数据库控制台,如mysql -uroot -p ...
- 一、Linux的基础使用--登录、开关机与在线、命令的查询帮助
目录 一.Linux的基础使用 1.1 X Window 与命令行模式的切换 1.2 命令行模式下命令的执行 1.3 修改支持语系 1.4 基础命令的操作 1.5 几个重要的热键[Tab].[Ctrl ...
- geth run
geth --networkid 1201 --rpc --rpcapi eth,net,web3,personal,admin,miner --rpccorsdomain "*" ...
- 用JS判断号码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue UI组件库
1. iView UI组件库 iView官网:https://www.iviewui.com/ 2.Vux UI组件库 Vux官网:https://vux.li/ 3.Element UI组件库 ...
- mac下更新node版本
node有一个专门管理node.js版本的包叫作:n: 查看当前 node版本:node -v 安装n工具包:sudo npm i -g n 安装最新版node.js:sudo n stable 安装 ...
- C之自定义类型
声明自定义数据类型,配合各种原有数据类型来达到简化编程的目的的类型定义关键字. #include<stdio.h> #include<stdlib.h> typedef int ...
- Hibernate fetch相关
fetch=FetchType.LAZY 时,spring boot jackson 返回数据时会出错. 可配置使用Hibernate4Module 帮助解决: @Configurationpubli ...
- django安装xadmin中出现的报错汇总
报错一:ModuleNotFoundError: No module named 'django.core.urlresolvers' ModuleNotFoundError: No module n ...
- uni-app 保持登录状态 (Vuex)
在小程序中,保持登录状态是很常见的需求,今天就把写一写使用uni-app框架的保持登录状态功能是怎样实现的. 一.场景需求 1.场景:初始打开---登陆---关闭,再次打开---(已登录)上次关闭前的 ...