0
请帮助下面。如何将元数据添加到Web Api响应
我需要返回API响应为IEnumerable类型的JSON响应,我需要我自己的JSON属性附加到这个返回对象的响应一起。
private async Task<IEnumerable<Funds>> GetFundDetails(int productid) { var action = $"productfunds/{productid}/detail"; var res = await HttpClient.GetAsync($"{BaseResource}{action}"); res.EnsureSuccessStatusCode(); var collection = await res.Content.ReadAsAsync<Dto.FundDetail>(); var items = (collection.Funds ?? Enumerable.Empty<Dto.Funds>()).Select<Dto.FundDetail, FundAsset>(MapFunds); return items; }
响应:
{
"items": [
{
"fundId": "036",
"fundName": "ABC Fund",
"amount": 1248111.26
},
{
"fundId": "037",
"fundName": "XYZ Fund",
"amount": 7858564.84
}
]
}
预期响应:
{
"items": [
{
"fundId": "036",
"fundName": "ABC Fund",
"amount": 1248111.26
},
{
"fundId": "037",
"fundName": "XYZ Fund",
"amount": 7858564.84
}
],
ProductName: "PQR Product"
ProductId: "1001"
}
- >我需要从集合对象其是Dto.FundDetail的属性附加产品名称和产品编号。
您遇到什么问题会阻止您提出的更改? – Jasen
如何通过附加产品名称和产品达到预期的响应 – user1926521
您提出的更改不再是可枚举的,而是包含一些属性和IEnumerable的新型模型。维护IEnumerable结果的一种方法是为每个'items'添加'ProductName'和'ProductId'。 – Jasen