小编Eli*_*099的帖子

Http请求如何返回对象列表

所以我已经从使用 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; }
}
Run Code Online (Sandbox Code Playgroud)

控制器

[HttpGet("getAllBrands")]
public async Task<IActionResult> Get()
{
    var brands = await _brandRepository.getAllBrands();

    List<Brand> list = (List<Brand>) brands;

    return new JsonResult(list);
} 
Run Code Online (Sandbox Code Playgroud)

品牌库

public async Task<IEnumerable<Brand>> getAllBrands()
{
    var brands = await _brands.Find(_ => true).ToListAsync();
    return brands;
}
Run Code Online (Sandbox Code Playgroud)

我的期望是什么

[
      {
        "_id": "60d235f60f8c98376ba5b67d",
        "name": "Some …
Run Code Online (Sandbox Code Playgroud)

c# api http

2
推荐指数
1
解决办法
2679
查看次数

标签 统计

api ×1

c# ×1

http ×1