Asp.Net Core 3.1 獲取不到Post、Put請求的內容 System.NotSupportedException Specified method is not supported


問題

是這樣的,我.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();

搞定:

1584806891769

問題不大,不過不想讓大家重復趟坑。

參考自:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM