[英]How to return a dynamic object list from web api in c#
I Have an object我有一个对象
public class GeneralResponseClass
{
public int ErrorCode { set; get; }
public string Date { set; get; }
public String ErrorMessage { set; get; }
public List<dynamic> Data = new List<dynamic>();
public bool Success { set; get; }
public GeneralResponseClass()
{
Date = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
}
}
in my API Function i want to return the object在我的 API 函数中,我想返回对象
[HttpGet]
public GeneralResponseClass Get()
{
GeneralResponseClass generalResponseClass = new GeneralResponseClass();
generalResponseClass.ErrorCode = 0;
generalResponseClass.ErrorMessage = "";
generalResponseClass.Success = false;
generalResponseClass.Data.Add(new
{
Name = "User 1",
Email = "User1@user.com"
});
generalResponseClass.Data.Add(new
{
Name = "User 2",
Email = "User2@user.com"
});
return generalResponseClass;
}
in the response i get don't get the List在我得到的回应中没有得到列表
{
errorCode: 0,
date: "06/01/2020 16:32:12",
errorMessage: "",
success: false
}
Why The List Data dont return?为什么列表数据不返回?
You cannot return any collection with anonymous
object.您不能使用
anonymous
对象返回任何集合。 You can use ExpandoObject
which is simillar to anonymous
.您可以使用与
anonymous
ExpandoObject
。 You can also create a custom class for your type of object since they have the same properties in your case (at least in the template).您还可以为您的对象类型创建自定义类,因为它们在您的案例中具有相同的属性(至少在模板中)。
You can check for more on ExpandoObject
here: What are the true benefits of ExpandoObject?您可以在此处查看有关
ExpandoObject
更多信息: ExpandoObject
的真正优势是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.