Hope everybody is having a good day. I am using an API to read from a DB and then testing this API with Postman.
I have a c# class that looks like this:
{
public class ResultValidSubscriptions
{
int ID;
string Integration;
string Domain;
int Month;
int Year;
int UserCount;
string AppVersion;
bool IsCorrect;
public ResultValidSubscriptions(int iD, string integration, string domain, int month, int year, int userCount, string appVersion, bool isCorrect)
{
ID = iD;
Integration = integration;
Domain = domain;
Month = month;
Year = year;
UserCount = userCount;
AppVersion = appVersion;
IsCorrect = isCorrect;
}
}
}
So I retrieve a list of these objects from the database and adding them to a list. I then use this method to return it to the caller(postman in this case).
[HttpGet]
public IHttpActionResult GetAllValidSubscriptions(int TargetYear, int TargetMonth)
{
try
{
arrayListSubscriptions = service.getAllValidSubscriptions(TargetYear, TargetMonth);
string json = JsonConvert.SerializeObject(arrayListSubscriptions);
var deserialized = JsonConvert.DeserializeObject<List<ResultValidSubscriptions>>(json);
return Ok(deserialized);
}
When I insert breakpoints I can see that arrayListSubscriptions contains the correct items. 5x ResultValidSubscriptions in this case. As you can see below the empty array also contains 5 items.
The problem is when I return either arrayListSubscriptions , Json, or deserialized all I get is an empty array.
When I change
return Ok(deserialized);
to
return Ok("hi");
It works fine and I see hi in postman. So my arraylist seems to be full, my method seems to be working. But somehow somewhere the array is getting cleared? I think the conversion to JSON is doing this, but I thought this would be solved by deserializing it with the class name.
Anybody that can help me with this? Much appreciated.
