将元组返回到具有更改参数名称的 API 调用



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

我返回一个包含和ID和一个name的元组。

List<Tuple<int, string>> tupleList= new List<Tuple<int, string>>();
foreach (Person person in people)
    tupleList.Add(person.GetIdName());

GetIdNamePeople 类中定义如下:

public Tuple<int, string> GetIdName()
{
    return new Tuple<int, string>(ID, nome);
}

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

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

我在客户端得到的是:

[{"m_Item1":1,"m_Item2":"Name 1"},{"m_Item1":2,"m_Item2":"Name 2"}]

有没有办法将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对象

相关内容

最新更新