在我的项目中,服务器返回空对象,而我判断了this.taskGateway.RetrieveSelection是否返回正确的数据:

[ApiController]
public class TaskController : ControllerBase
{
    private readonly TaskGateway taskGateway = FrontServerDependencies.Injector.gateways().Task;
  
    [HttpGet(TasksTransactions.RetrievingOfSelection.URN_PATH)]
    public async System.Threading.Tasks.Task<
      ActionResult<TaskGateway.SelectionRetrieving.ResponseData>
    > Get() 
    {
        return base.Ok(
            await this.taskGateway.RetrieveSelection(
                new TaskGateway.SelectionRetrieving.RequestParameters
                {
                    OnlyTasksWithAssociatedDate = onlyTasksWithAssociatedDate,
                    OnlyTasksWithAssociatedDateTime = onlyTasksWithAssociatedDateTime,
                    SearchingByFullOrPartialTitleOrDescription = searchingByFullOrPartialTitle
                })
        );
    }
}

AFAIK可能会遗漏两件事:

  1. 入口点中的某些内容尚未初始化
  2. 控制器方法中出现了一些错误

我在查official Microsoft tutorial美元.

还没有发布入口点的列表,所以我需要初始化ASP.NET Core Web API项目并判断Program.cs.没有提到任何与JSON序列化相关的内容:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();

因此,它看起来不像是第一个.

判断示例的控制器:

using Microsoft.AspNetCore.Mvc;

namespace ASP_DOT_NET_CoreWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            }).ToArray();
        }
    }
}

Get方法返回对象的array. 同样,与JSON序列化无关.

为什么在我的项目--不是--的时候,这个例子可以工作?

TaskGateway.SelectionRetrieving.ResponseData名分别是:

struct ResponseData
{
    public required uint TotalItemsCount;
    public required uint TotalItemsCountInSelection;
    public required CommonSolution.Entities.Task[] Items;
}

推荐答案

documentation开始,以下是序列化行为:

  • 默认情况下,所有公共属性都是序列化的.您可以指定要忽略的属性.您还可以包括私有成员.

  • 默认情况下,将忽略字段.您可以包括字段.

因此,将字段更改为属性,如下所示:

struct ResponseData
{
    public required uint TotalItemsCount { get; set; }
    public required uint TotalItemsCountInSelection { get; set; }
    public required CommonSolution.Entities.Task[] Items  { get; set; }
}

Csharp相关问答推荐

使用GeneratedComInterfaceProperty的.NET 8 COM类对于VB 6/SYS或ALEViewer不可见

禁用AutoSuggestBox项目更改时的动画?

Blazor:类型或命名空间名称Components在命名空间中不存在''

C#中使用BouncyCastle计算CMac

如何修改中间件或其注册以正确使用作用域服务?

不仅仅是一个简单的自定义按钮

Unity中的刚体2D运动

在不添加不必要的尾随零的情况下本地化浮点型?

VS 2022 for ASP.NET Core中缺少自定义项模板

当试图限制EF Select 的列时,如何避免重复代码?

基于C#和ANGING的SignalR实时聊天流媒体应用

BFF到具有AAD/Entra ID B2C的内部API(.NET/ASP.NET核心/标识.Web)

.Net MAUI,在将FlyoutPage添加到容器之前,必须设置添加构造函数导致Flyout和Detail"

MSI无法将快捷方式添加到启动文件夹

.NET Google Workspace API获取错误CS0266

如何使用.NET Aspire从Blazor应用程序与GRPC API通信?

为什么在使用JsonDerivedType序列化泛型时缺少$type?

如何在Cake脚本中设置MSBuild.exe的绝对路径

最小API定义的Swagger标头参数

在使用xUnit和Mock执行单元测试时,控制器ViewResult返回空的Model集合