ASP.NET 核心 3.1 Web API - 自定义 json
ASP.NET Core 3.1 Web API - custom json
在我的项目中,我有一个名为 product (Id, name)
的 table,它有 10K 多行。我会 return JSON 以下格式的对象。
我在 C# (Id, name)
中有一个 Product
class。当我做 Http get it return [{id: "", name:""},{id: "", name:""}]
。我想扭曲 "Product" :[]
的 return 数组
当前return是
[
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Laptop"
},
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Mobile"
}
]
想要的结果是:
"product":
[
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Laptop"
},
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Mobile"
}
]
请指教
1.you可以尝试直接使用下面的代码:
return Ok(new { products = _mapper.Map<IEnumerable<Models.ProductDto>>(proRepo) });
2.Another方式,你可以尝试创建
public class ViewModel
{
public List<Product> product { get; set; }
}
然后使用
return Ok(new ViewModel{ products = _mapper.Map<IEnumerable<Models.ProductDto>>(proRepo) });
在我的项目中,我有一个名为 product (Id, name)
的 table,它有 10K 多行。我会 return JSON 以下格式的对象。
我在 C# (Id, name)
中有一个 Product
class。当我做 Http get it return [{id: "", name:""},{id: "", name:""}]
。我想扭曲 "Product" :[]
当前return是
[
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Laptop"
},
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Mobile"
}
]
想要的结果是:
"product":
[
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Laptop"
},
{
"id": "3089e0d5-476f-407a-9458-949d0d08123f"
"name": "Mobile"
}
]
请指教
1.you可以尝试直接使用下面的代码:
return Ok(new { products = _mapper.Map<IEnumerable<Models.ProductDto>>(proRepo) });
2.Another方式,你可以尝试创建
public class ViewModel
{
public List<Product> product { get; set; }
}
然后使用
return Ok(new ViewModel{ products = _mapper.Map<IEnumerable<Models.ProductDto>>(proRepo) });