前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Asp.Net Core 3.1 获取不到Post、Put请求的内容 System.NotSupportedException Specified method is not supported

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

作者头像
乔达摩@嘿
发布2020-09-11 16:06:43
2.5K0
发布2020-09-11 16:06:43
举报
文章被收录于专栏:嘿dotNet嘿dotNet

问题

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

代码语言:javascript
复制
[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的内容:

代码语言:javascript
复制
curl --location --request POST 'http://localhost:5001/api/TestPostReader/test' \
--header 'Content-Type: application/json' \
--data-raw '{"name":"hei"}'

报异常:

代码语言:javascript
复制
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 这里改成这样:

代码语言:javascript
复制
app.Use((context, next) =>
{
    context.Request.EnableBuffering();
    return next();
});
//这个在后边
app.UseRouting();

搞定:

1584806891769
1584806891769

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

参考自:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-03-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 解决
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档