C# 使用更改的参数名称将元组返回到API调用

C# 使用更改的参数名称将元组返回到API调用,c#,asp.net-mvc,tuples,C#,Asp.net Mvc,Tuples,我想我无法在标题中更好地解释它。 对不起 我将返回一个元组,其中包含和ID以及一个名称 现在,所有这些都是由客户端通过API调用的 [ResponseType(typeof(List<Tuple<int, string>>))] public IHttpActionResult GetPeople(int id) { return Ok(tupleList) } 有没有办法将m_Item1设置为其他值?如果您能够更改定义,请创建如下视图模型 public cla

我想我无法在标题中更好地解释它。 对不起

我将返回一个元组,其中包含和ID以及一个名称

现在,所有这些都是由客户端通过API调用的

[ResponseType(typeof(List<Tuple<int, string>>))]
public IHttpActionResult GetPeople(int id)
{
    return Ok(tupleList)
}

有没有办法将m_Item1设置为其他值?

如果您能够更改定义,请创建如下视图模型

public class MyViewModel
{
    public int Id { get; set; }
    public string name { get; set; }
}
所以你的代码是

List<MyViewModel> tupleList= new List<MyViewModel>();
foreach (Person person in people)
    tupleList.Add(person.GetIdName());

同时,也相应地更改方法GetIdName,以返回MyViewModel对象,而不是元组。为什么不能使用ViewModel呢
[{"m_Item1":1,"m_Item2":"Name 1"},{"m_Item1":2,"m_Item2":"Name 2"}]
public class MyViewModel
{
    public int Id { get; set; }
    public string name { get; set; }
}
List<MyViewModel> tupleList= new List<MyViewModel>();
foreach (Person person in people)
    tupleList.Add(person.GetIdName());