gpt4 book ai didi

C# web api 返回列表对象的列表

转载 作者:行者123 更新时间:2023-11-30 23:27:11 26 4
gpt4 key购买 nike

我有一个典型的 react

ResultModel
{
Bool Result,
Object Response
}

我想发送例如带有产品的类别

var response = new List<Category> 
{
new Category {Name = "1", Products = new List<Product> {new Product {Name = "1_1"}, new Product {Name = "1_2"}} },
new Category {Name = "2", Products = new List<Product> {new Product {Name = "2_1"}, new Product {Name = "2_2"}} },
new Category {Name = "3", Products = new List<Product> () },
}

还有我的回应

return new ResultModel
{
Response = response,
Result = true
};

但我无法收到 Products 属性作为响应。有人知道为什么吗?谢谢

最佳答案

您只能获取属性 Name,因为它是原始类型并且序列化程序知道如何对其进行序列化和反序列化。如果您想获得更复杂的对象,您必须确保序列化程序知道如何去做。

您可以在您的对象中实现 Iserializable 接口(interface),并且在您确定可以序列化它之后,您可以在您的 webApi 中使用它。

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable(v=vs.110).aspx

这里有一个关于这个 Using Serializable attribute on Model in WebAPI 的 SO 线程

关于C# web api 返回列表对象的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36616093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com