2017-08-24 32 views
0

请帮助下面。如何将元数据添加到Web Api响应

  1. 我需要返回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的属性附加产品名称和产品编号。

+0

您遇到什么问题会阻止您提出的更改? – Jasen

+0

如何通过附加产品名称和产品达到预期的响应 – user1926521

+0

您提出的更改不再是可枚举的,而是包含一些属性和IEnumerable的新型模型。维护IEnumerable结果的一种方法是为每个'items'添加'ProductName'和'ProductId'。 – Jasen

回答

0

您应该包含Dto.Funds新模式,ProductIdProductName, 财产以后这样的:

public class NewModelToReturn{ 

    public List<Dto.Funds> Funds{get;set;} 
    public string ProductName; 
    public string ProductId; 

} 

顺便说一句,我也鼓励您阅读本矿博客张贴有关设计ASP.NET网页API回复以了解更多关于返回一致的Web Api响应(成功和失败响应):https://www.vladopandzic.com/asp-net-web-api/building-consistent-responses-asp-net-web-api/

相关问题