'ASP.NET Core 6 : server returning proper array length but empty data
I am using ASP.NET Core 6. I have a controller that is returning the proper number of items in the array, however the objects themselves are empty.
ChartController.cs
[ApiController]
public class ChartController : ControllerBase
{
[HttpGet]
[Route("charts/{symbol:alpha}")]
public IEnumerable<ChartDataOhlc> GetChartData(string symbol)
{
List<ChartDataOhlc> data = new List<ChartDataOhlc>()
{
new() { DateTime = DateTime.Now, Open = "1", High = "5", Low = "0", Close = "4" },
new() { DateTime = DateTime.Now, Open = "1", High = "5", Low = "0", Close = "4" },
new() { DateTime = DateTime.Now, Open = "1", High = "5", Low = "0", Close = "4" },
new() { DateTime = DateTime.Now, Open = "1", High = "5", Low = "0", Close = "4" },
new() { DateTime = DateTime.Now, Open = "1", High = "5", Low = "0", Close = "4" },
};
// list2.Add(new Test() { A = 1, B = "B1" });
return (data);
}
}
ChartDataOhlc.cs
public class ChartDataOhlc
{
public DateTime DateTime;
public string Open = null!;
public string High = null!;
public string Low = null!;
public string Close = null!;
}
But all I'm getting is [{},{},{},{},{}]
Solution 1:[1]
Replace ChartDataOhlc class fields with properties like :
public class ChartDataOhlc
{
public DateTime DateTime {get; set;}
public string Open {get; set;} = null!;
public string High {get; set;} = null!;
public string Low {get; set;} = null!;
public string Close {get; set;} = null!;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rohit Jadhav |
