返回如何从 Web API 中以 C# 返回动态对象列表。

huangapple go评论104阅读模式
英文:

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

[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 doesn't the List<dynamic> Data return?

英文:

I Have an object

public class GeneralResponseClass
{
    public int ErrorCode { set; get; }
    public string Date { set; get; }
    public String ErrorMessage { set; get; }

    public List&lt;dynamic&gt; Data = new List&lt;dynamic&gt;();
    public bool Success { set; get; }
    public GeneralResponseClass()
    {
        Date = DateTime.Now.ToString(&quot;dd/MM/yyyy HH:mm:ss&quot;);
    }
}

in my API Function i want to return the object

[HttpGet]
public GeneralResponseClass Get()
    {
        GeneralResponseClass generalResponseClass = new GeneralResponseClass();

        generalResponseClass.ErrorCode = 0;
        generalResponseClass.ErrorMessage = &quot;&quot;;

        
        generalResponseClass.Success = false;



        generalResponseClass.Data.Add(new
        {
            Name = &quot;User 1&quot;,
            Email = &quot;User1@user.com&quot;
        });

        generalResponseClass.Data.Add(new
        {
            Name = &quot;User 2&quot;,
            Email = &quot;User2@user.com&quot;
        });

        return generalResponseClass;
    }

in the response i get don't get the List

 {
   errorCode: 0,
date: &quot;06/01/2020 16:32:12&quot;,
errorMessage: &quot;&quot;,
success: false
}

Why The List<dynamic> Data dont return?

答案1

得分: 2

你不能返回带有“anonymous”对象的集合。您可以使用类似于“anonymous”的“ExpandoObject”。您还可以为您的对象类型创建一个自定义类,因为它们在您的情况下具有相同的属性(至少在模板中是这样的)。

您可以在这里查看更多关于“ExpandoObject”的信息:https://stackoverflow.com/questions/1653046/what-are-the-true-benefits-of-expandoobject

英文:

You cannot return any collection with anonymous object. You can use ExpandoObject which is simillar to anonymous. 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: https://stackoverflow.com/questions/1653046/what-are-the-true-benefits-of-expandoobject

huangapple
  • 本文由 发表于 2020年1月6日 22:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定