So I have moved from working with Java to C# and man is Spring boot different from C#. I am trying to return a list of objects to a rest API using mongodb database. But the result does not return a list, but rather some object containing my list.
Brand class
[BsonIgnoreExtraElements]
public class Brand
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string _id { get; set; }
public string name { get; set; }
}
Controller
[HttpGet("getAllBrands")]
public async Task<IActionResult> Get()
{
var brands = await _brandRepository.getAllBrands();
List<Brand> list = (List<Brand>) brands;
return new JsonResult(list);
}
Brand Repository
public async Task<IEnumerable<Brand>> getAllBrands()
{
var brands = await _brands.Find(_ => true).ToListAsync();
return brands;
}
What I expect
[
{
"_id": "60d235f60f8c98376ba5b67d",
"name": "Some brand 1"
},
{
"_id": "60d24d6b0f8c98376ba5b68c",
"name": "Some brand 2"
},
{
"$id": "6",
"_id": "60d24e4b0f8c98376ba5b68d",
"name": "Some brand 3"
}
]
What I'm actually getting
{
"$id": "1",
"$values": [
{
"$id": "2",
"_id": "60d235f60f8c98376ba5b67d",
"name": "Some brand 1"
},
{
"$id": "4",
"_id": "60d24d6b0f8c98376ba5b68c",
"name": "Some brand 2"
},
{
"$id": "6",
"_id": "60d24e4b0f8c98376ba5b68d",
"name": "Some brand 1"
}
]
}
How do I just return a simple list of objects as my result and not that? Thanks