The application completed without reading the entire request body, .net core 2.1.1

后端 未结 10 1353
-上瘾入骨i
-上瘾入骨i 2020-12-14 07:21

I have created a user register controller to register users with repository design pattern. My controller looks like this.

[Route(\"api/[controller]\")]
             


        
相关标签:
10条回答
  • 2020-12-14 07:28

    It happened to me in a new ASP.NET Core 2.1 service when debugging in localhost because I had in Startup.Configure:

    app.UseHttpsRedirection();
    

    I deactivated this setting when debugging locally:

    if (env.IsDevelopment())
    {
         app.UseDeveloperExceptionPage();
    }
    else
    {
         app.UseHttpsRedirection();
    }
    
    0 讨论(0)
  • 2020-12-14 07:37

    I had the same error (even with "Content-Type: application/json") but adding "{id}" into the action verb worked for me i.e. changing from

        [HttpPatch]
        [ActionName("Index")]
        [Authorize(Policy = "Model")]
        public async Task<JsonResult> Update([FromRoute]int id, int modelId, [FromBody]Device device)
    

    to

        [HttpPatch("{id}")]
        [ActionName("Index")]
        [Authorize(Policy = "Model")]
        public async Task<JsonResult> Update([FromRoute]int id, int modelId, [FromBody]Device device)
    

    (asp.net core 2.1)

    0 讨论(0)
  • 2020-12-14 07:38

    Could you try it by adding a request method [Route("jsonbody")]

     [AllowAnonymous]
     [HttpPost("register")]
     [Route("jsonbody")]
        public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto){}
    
    0 讨论(0)
  • 2020-12-14 07:40

    The error info of the application completed without reading the entire request body often occurs when the client send a request that doesn't fulfill the sever requirements . In other words , it happens just before entering the action , resulting that you cannot debug it via a breakpoint within the body of action method .

    For example , let's say a action method on the server :

    [Route("api/[controller]")]
    [ApiController]
    public class DummyController : ControllerBase
    {
        [HttpPost]
        public DummyDto PostTest([FromBody] DummyDto dto)
        {
            return dto;
        }
    }
    

    The DummyDto here is a dummy class to hold information:

    public class DummyDto 
    {
        public int Id { get; set; }
    }
    

    When clients send a request with payload not well formatted

    For example , the following post request , which doesn't have a Content-Type: application/json header :

    POST https://localhost:44306/api/test HTTP/1.1
    Accept : application/json
    
    { "id":5 }
    

    will result in a similar error info :

    Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44306/api/test  10
    Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1.9319ms 404 
    Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLGH8R93RPUO", Request id "0HLGH8R93RPUO:00000002": the application completed without reading the entire request body.
    

    and the response from the server will be 404:

    HTTP/1.1 404 Not Found
    Server: Kestrel
    X-SourceFiles: =?UTF-8?B?RDpccmVwb3J0XDIwMThcOVw5LTFcU08uQXV0aFJlYWRpbmdXaXRob3V0RW50aXRlQm9keVxBcHBcQXBwXGFwaVx0ZXN0?=
    X-Powered-By: ASP.NET
    Date: Mon, 03 Sep 2018 02:42:53 GMT
    Content-Length: 0
    

    As for the question you described , I suggest you should check the following list :

    1. does the Postman send the request with a header of Content-Type: application/json ? make sure you have checked the header
    2. If step1 doesn't work , click the code to show what it sends exactly when you send a request to the server .
    0 讨论(0)
  • 2020-12-14 07:40

    There can be multiple reasons out of which one can be : – Caching in Visual Studio --

    1.Close all the instances of visual studios, run Developer command prompt with Admin rights.
    2.git clean -xfd [Your Repository to remove all dependencies and existing soln file]
    3.take the latest build and run . [Make Endpoint AllowAnonymous]
    
    0 讨论(0)
  • 2020-12-14 07:41

    I solved it like that. From

    namespace AuthenticationService.Controllers
    {
        [Route("api/authentication")]
        [ApiController]
        public class AuthenticationController : ControllerBase
        {
            [HttpPost("/token")]
            public IActionResult GenerateToken([FromBody] LoginRest loginRest)
            {
    

    to [Route("api/authentication/")] with an addional /. The slash at[HttpPost("token")] I removed.

    0 讨论(0)
提交回复
热议问题