所以我已经从使用 Java 转向 C#,而人是 Spring boot 与 C# 不同。我正在尝试使用 mongodb 数据库将对象列表返回到 rest API。但结果并没有返回一个列表,而是一些包含我的列表的对象。
品牌类
[BsonIgnoreExtraElements]
public class Brand
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string _id { get; set; }
public string name { get; set; }
}
控制器
[HttpGet("getAllBrands")]
public async Task<IActionResult> Get()
{
var brands = await _brandRepository.getAllBrands();
List<Brand> list = (List<Brand>) brands;
return new JsonResult(list);
}
品牌库
public async Task<IEnumerable<Brand>> getAllBrands()
{
var brands = await _brands.Find(_ => true).ToListAsync();
return brands;
}
我的期望
[
{
"_id": "60d235f60f8c98376ba5b67d",
"name": "Some brand 1"
},
{
"_id": "60d24d6b0f8c98376ba5b68c",
"name": "Some brand 2"
},
{
"$id": "6",
"_id": "60d24e4b0f8c98376ba5b68d",
"name": "Some brand 3"
}
]
我实际上得到了什么
{
"$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"
}
]
}
我如何只返回一个简单的对象列表作为我的结果而不是那个?谢谢