'How can I return values of each object wihtin List of Object using API
I have created an object called UserModel. I have also created a method that returns a List of User Models within a database. Below is my method for returning Users from database.
static public List<UserModel> SearchUsers(string tableName)
{
string commandString = $"SELECT* FROM [{tableName}]";
using SqlConnection connection = new(ConnectionSQL.connectionString);
connection.Open();
using SqlCommand command = new(commandString, connection);
//Console.WriteLine(command);
using SqlDataReader reader = command.ExecuteReader();
var users = new List<UserModel>();
if (reader.HasRows)
{
while (reader.Read())
{
users.Add(new UserModel
{
ID = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
Email = reader.GetString(3),
Phone = reader.GetString(4),
Password = reader.GetString(5),
});
}
}
return users;
}
Now this is the code I am using for my API. I am confused on how I would be able to get an api to read the objects within the list.
[ApiController]
[Route("[controller]")]
public class Users : ControllerBase
{
[HttpGet(Name = "UsersSearch")]
public ActionResult<List<UserModel>> Get()
{
List<UserModel> myValues = DL.DLUser.SearchUsers("Users");
return myValues;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
