Skip to content

Question about ActionResult<IEnumerable<Something>> #3224

Closed
@jhgann

Description

@jhgann

I have been working on updating to .NET Core 2.1 on my api controllers. I've changed my IActionResult to ActionResult, and removed the Ok() and just return the T object now. But it doesn't seem to work with an IEnumerable.
Examples:
This works:

[HttpGet]
public async Task<IActionResult> GetUsersInPool(string poolId)
{
   // do work to get users

    return Ok(pool.Users);
}

This also works:

[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsersInPool(string poolId)
{
    // do work to get users

    return Ok(pool.Users);
}

This also works

[HttpGet]
public async Task<ActionResult<List<User>>> GetUsersInPool(string poolId)
{
    // do work to get users

    return new List<User>(pool.Users);
}

This gives me a compile error:

[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsersInPool(string poolId)
{
     // do work to get users

    return pool.Users;
}

Error: (on return pool.Users;)
CS0029 Cannot implicitly convert type 'System.Collections.Generic.ICollection<License.API.Entities.User>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<License.API.Entities.User>>

Is there something I don't understand about ActionResult, or IEnumerable? Is the proper solution to covert to list first?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions