ASP.NET 核心 6:服务器返回正确的数组长度但返回空数据
ASP.NET Core 6 : server returning proper array length but empty data
我正在使用 ASP.NET Core 6。我有一个控制器返回数组中正确数量的项目,但是对象本身是空的。
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!;
}
但我得到的只是[{},{},{},{},{}]
将 ChartDataOhlc
class 字段替换为如下属性:
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!;
}
我正在使用 ASP.NET Core 6。我有一个控制器返回数组中正确数量的项目,但是对象本身是空的。
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!;
}
但我得到的只是[{},{},{},{},{}]
将 ChartDataOhlc
class 字段替换为如下属性:
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!;
}