Home > Blockchain >  ASP.NET Core Asynchronous API Endpoint Return Extra Properties beside Actual Payload Data
ASP.NET Core Asynchronous API Endpoint Return Extra Properties beside Actual Payload Data

Time:12-01

I am developing the back end API in ASP.NET Core 5.0 with C#.

I see when using async method for my API endpoint response are return with extra properties like "exception", "status", "isCancelled", "result" etc. preparty. In "result" property the expected response data returned.

Returned JSON:

{
"result": [
    {
        "id": 1,
        "username": "bette",
        "age": 40
    },
    {
        "id": 2,
        "username": "cherry",
        "age": 65
    }
],
"id": 1,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}

API Endpoint Repository Code:

public async Task<IEnumerable<MemberDto>> GetMembersAsync()
{
    return await _context.Users
         .ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
         .ToListAsync();
}

Actual API Controller Endpoint:

[HttpGet]
public async Task<ActionResult<IEnumerable<MemberDto>>> GetUsers()
{
    var users = _userRepository.GetMembersAsync();
    return Ok(users);
}

Why getting such extra properties from the API?

CodePudding user response:

Your controller endpoint should be:

[HttpGet]
public async Task<ActionResult<IEnumerable<MemberDto>>> GetUsers()
{
    var users = await _userRepository.GetMembersAsync();
    return Ok(users);
}
  • Related