[英]ASP.Net Core 3.1 WEB API - Controller method doesn't return correct JSON
I have an ASP.Net Core 2.2 Website that I have upgraded to ASp.Net Core 3.0.我有一个已升级到 ASp.Net Core 3.0 的 ASP.Net Core 2.2 网站。 Most of the WEB API methods works as expected and returns data just as before the upgrade.
大多数 WEB API 方法按预期工作,并像升级前一样返回数据。
But this API methods returns an array with empty elements:但是这个 API 方法返回一个包含空元素的数组:
[Route("api/TripAnalysis/{id:int}")]
public List<TurAnalyse> GetTripAnalysis(int id)
{
var x = new List<TurAnalyse>() { new TurAnalyse { ArtId = 1, ArtNavn = "Havørn", C1 = "X", Su = false } };
return x;
}
The TurAnalyse model class is here: TurAnalyse model class 在这里:
public class TurAnalyse
{
public int ArtId;
public bool Su;
public string ArtNavn;
public string C1;
public string C2;
public string C3;
public string C4;
public string C5;
public string C6;
}
I get this back when calling the controller method:调用 controller 方法时,我得到了回复:
[{}]
So it returns an array with 1 element, but no properties or values - and don't understand why?所以它返回一个包含 1 个元素的数组,但没有属性或值 - 不明白为什么?
The default JSON Serializer in .NET Core 3.1 does not support serializing fields. .NET Core 3.1 中的默认 JSON 序列化程序不支持序列化字段。 So either switch them to properties, switch to JSON.NET, or jump to .NET Core 5 which does support fields.
因此,要么将它们切换到属性,切换到 JSON.NET,要么跳转到支持字段的 .NET Core 5。 eg
例如
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(o => o.JsonSerializerOptions.IncludeFields = true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.