我想创建向Angular发送输入的WEB API。我想以JSON格式发送数据作为数组。
以下是我的代码:
[HttpGet]
[ActionName("GetEmployeeByID")]
public Employee Get(int id)
{
Employee emp = null;
while (reader.Read())
{
emp = new Employee();
emp.ClientId = Convert.ToInt32(reader.GetValue(0));
emp.ClientName = reader.GetValue(1).ToString();
}
return emp;
}
实际输出:
{"ClientId":15,"ClientName":"Abhinav Singh"}
预期输出:
[{"ClientId":15,"ClientName":"Abhinav Singh"}]
答案 0 :(得分:3)
您的代码只返回一个元素。使用 List
更改它以返回集合,如下所示,
public List<Employee> Get(int id)
{
Employee emp = null;
List<Employee> _employees = new List<Employee>();
while (reader.Read())
{
emp = new Employee();
emp.ClientId = Convert.ToInt32(reader.GetValue(0));
emp.ClientName = reader.GetValue(1).ToString();
_employees.Add(emp);
}
return _employees;
}