问题

是这样的,我.net core 2.1的项目,读取、获取Post请求内容的一段代码,大概这样:

[HttpPost]
public async Task<IActionResult> Test([FromBody]string postStr)
{ using (var reader = new StreamReader(Request.Body, System.Text.Encoding.UTF8))
{
reader.BaseStream.Seek(0, SeekOrigin.Begin); //大概是== Request.Body.Position = 0;的意思
var readerStr = await reader.ReadToEndAsync();
reader.BaseStream.Seek(0, SeekOrigin.Begin); //读完后也复原 return Ok(readerStr);
}
}

但这段代码 在 .net core 3.1.0 和 .net core 3.1.2(没错特地升级过) 都读不到、获取不到Post的内容:

curl --location --request POST 'http://localhost:5001/api/TestPostReader/test' \
--header 'Content-Type: application/json' \
--data-raw '{"name":"hei"}'

报异常:

System.NotSupportedException: Specified method is not supported.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Seek(Int64 offset, SeekOrigin origin)
at Push.WebApi.Controllers.TestPostReaderController.Test() in D:\工作\39\solution-push\Push.WebApi\Controllers\TestPostReaderController.cs:line 21
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

解决

StartUp Configure 这里改成这样:

app.Use((context, next) =>
{
context.Request.EnableBuffering();
return next();
});
//这个在后边
app.UseRouting();

搞定:

问题不大,不过不想让大家重复趟坑。

参考自:

https://github.com/dotnet/aspnetcore/issues/15009

Asp.Net Core 3.1 获取不到Post、Put请求的内容 System.NotSupportedException Specified method is not supported的更多相关文章

  1. ASP.NET Core 注入和获取 AppSettings 配置

    ASP.NET Core 项目中有个appsettings.json配置文件,用于存放一些配置信息,比如数据库连接字符串等,但访问的话,只能在 ASP.NET Core 项目中获取,如果我们在其他项目 ...

  2. Asp.Net Core 减少Controller获取重复注入对象

    原文:Asp.Net Core 减少Controller获取重复注入对象 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012770274/art ...

  3. 帅呆了!ASP.NET Core每秒能处理115万个请求

    今天看到一篇英文博文 -- ASP.NET Core – 2300% More Requests Served Per Second,被震撼了!ASP.NET Core每秒能处理115万个请求(是的, ...

  4. ASP.NET Core Web APi获取原始请求内容

    前言 我们讲过ASP.NET Core Web APi路由绑定,本节我们来讲讲如何获取客户端请求过来的内容. ASP.NET Core Web APi捕获Request.Body内容 [HttpPos ...

  5. ASP.NET Core DI 手动获取注入对象

    ASP.NET Core DI 一般使用构造函数注入获取对象,比如在ConfigureServices配置注入后,通过下面方式获取: private IValueService _valueServi ...

  6. ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action (转载)

    ASP.NET MVC 一.获取URL(ASP.NET通用): [1]获取完整url(协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [ ...

  7. asp.net core 3.0获取web应用的根目录

    目录 1.需求 2.解决方案 1.需求 asp.net core 3.0的web项目中,在controller中,想要获取wwwroot下的imgs/banners文件夹下的所有文件: 在传统的asp ...

  8. asp dotnet core 从 Frp 获取用户真实 IP 地址

    我在本地开一个服务,然后通过 Frp 让小伙伴可以在外网访问我的 API 连接,但是直接通过 RemoteIp 拿到的是本地的地址.本文告诉小伙伴如何通过 Frp 可以拿到用户的真实 IP 地址 我写 ...

  9. ASP.NET Core MVC 如何获取请求的参数

    一次HTTP请求,就是一次标准IO操作.请求是I,是输入:响应式O,是输出.任何web开发框架,其实都是在干这两件事: 接受请求并进行解析获取参数 根据参数进行渲染并输出响应内容 所以我们学习一个框架 ...

随机推荐

  1. 微信小游戏广告位iphonex底部适配问题

    最近在公司开发游戏,使用cocos creator做微信小游戏,遇到一个很恶心的问题,如图: 如图所示,微信的广告位被iphonex的底部bar给弹出了一点位置,没有靠在底部. 在这里不得不吐槽一下微 ...

  2. 组合数学--容斥原理&鸽巢原理

    一次会议由1990位数学家参加,每人至少有1327位合作者.证明可以找到4位数学家,他们当中每两个人都合作 优质解答 这题可以分两步来做.第一,先证明一定有三个人,他们相互合作过.可以先找两个相互合作 ...

  3. linux公社大量免费的在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 下载如需密码,详见博客案例:点击我去查看密码 2011年9月12日 21: ...

  4. linux系统文件被删的几种恢复方法

    参考链接:https://www.cnblogs.com/276815076/p/5703796.html 1.几种恢复方法,这里只是记录别的博客提到的方法,本人并未亲自验证. ext3用ext3gr ...

  5. windowserver 2012安装openssh

    下载https://github.com/PowerShell/Win32-OpenSSH/releases解压放到C:\Program Files\OpenSSH-Win64 进入到C:\Progr ...

  6. linux安装部署ftp图片服务器

    1.安装http反向代理服务器.安装ftp文件传输组件vsftpd 详细安装及配置参见 https://blog.csdn.net/zhouym/article/details/100145964 2 ...

  7. Design Patterns | 01 为什么要尽早掌握设计模式

    标题:Design Patterns | 01 为什么要尽早掌握设计模式 链接: 标签:设计模式 摘要:设计模式是前人经验的总结,教大家如何写出可扩展.可读.可维护的高质量代码.设计模式与日常工作中的 ...

  8. c#百度IP定位API使用方法

    c#百度IP定位API使用方法 1.先建立一个收集信息的实体类 IPModel.cs: using System; using System.Collections.Generic; using Sy ...

  9. 正式学习MVC 02

    1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...

  10. All In One

    set1 https://github.com/tianhang-f... set2 https://github.com/tianhang/F... set3https://github.com/t ...