在我的项目中,服务器返回空对象,而我判断了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可能会遗漏两件事:
- 入口点中的某些内容尚未初始化
- 控制器方法中出现了一些错误
我在查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;
}