简体   繁体   English

ASP.Net Core 3.1 WEB API - Controller 方法不返回正确的 Z0ECD11C1D7A287401F814A8

[英]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.

相关问题 "ASP.NET Core 3.1:Web Api:相同的发布方法:多种类型的 Json 对象" - ASP.NET Core 3.1: Web Api: Same Post Method: Multiple types of Json Objects Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? - Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? 在 ASP.NET Core 2.0 Web Api 中返回“原始”json - Return “raw” json in ASP.NET Core 2.0 Web Api 无法访问 ASP.NET Core 3.1 Web API Z9BBF373797BF7CF7BA6ZC8002 中的端点,5 - Unable to access an endpoint in ASP.NET Core 3.1 Web API Controller, from View ASP.NET Core 3.1 Web API post 方法导致 405“Method Not Allowed”错误 - ASP.NET Core 3.1 Web API post method causing 405 "Method Not Allowed" error 为什么ASP.NET Core Web API中没有这种死锁? - Why doesn't this deadlock in ASP.NET Core Web API? ASP.NET Core 3.1 Web API JSON 响应骆驼化超过第一个字符 - ASP.NET Core 3.1 web API JSON response camelizes more than first character 带有自定义响应包装器的 ASP.NET Core 3.1 Web API 中的 JSON 响应中断 - JSON response breaks in ASP.NET Core 3.1 Web API with custom response wrapper ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in Asp.net 核心 3.1 保护 API 和 web 应用程序 - Asp.net core 3.1 securing API and web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM