Problem: I was writing API in .NET Core 3.1. The API's exported list of custom classes for [httpget] methods from controller. I used Postman for testing the API's. And observed that from code the list of classes were returned correctly but it were always coming as empty entries in Post man result window.
Solution: The class was returning list as Action result. The native data types like int or string lists were working fine. On further debugging I noticed that the class members were declared public. The get and set modifiers were missing from the class. After adding the get and set methods to the data members of the class. For example:
The following code was not working
// GET: api/<Controller>
[HttpGet("Sample/Run")]
public async Task<ActionResult> Sampletest()
{
var result = await _datafromDB.GetDaataFromDB();
return Ok(result);
}
public async Task<List<MyClass>> GetDaataFromDB()
{
try
{
List<MyClass> list = new List<MyClass>();
await foreach (var data in StoredProc.GetDataAsync("EXEC USP_GETMYDATA"))
{
list.Add(new MyClass
{
Member1 = data.memebr1;
memeber2= data.memeber2;
}
}
public MyClass
{
public double Member1;
public double Memebr2
}
To make the above code work from Postman, change the model class to following
public MyClass
{
public double Member1 {get;set;}
public double Memebr2 {get;set;}
}
Comments
Post a Comment