Я включил полный список простого приложения, которое я создал, чтобы проверить, смогу ли я воспроизвести такое поведение. Приложение включает в себя две конечные точки, которые должны возвращать один и тот же объект.
Код: Выделить всё
using Newtonsoft.Json;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
School school = new School();
school.SchoolName = "My Elementary School";
school.Students.Add(new Student() { FirstName = "Bill", LastName = "Smith", Grade = 4, StudentID = "123456" });
school.Students.Add(new Student() { FirstName = "Jane", LastName = "Doe", Grade = 5, StudentID = "54321" });
app.MapGet("/SchoolsV1", () =>
{
return Results.Ok(school);
});
app.MapGet("/SchoolsV2", () =>
{
string strValue = JsonConvert.SerializeObject(school, Formatting.Indented);
return Results.Text(strValue, "application/json", null);
});
app.Run();
public class School
{
public string SchoolName { get; set; }
public List Students = new List();
}
public class Student
{
public string StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Grade { get; set; }
}
Код: Выделить всё
https://localhost:7000/SchoolsV1Код: Выделить всё
{
"schoolName": "My Elementary School"
}
Код: Выделить всё
https://localhost:7000/SchoolsV2Код: Выделить всё
{
"Students": [
{
"StudentID": "123456",
"FirstName": "Bill",
"LastName": "Smith",
"Grade": 4
},
{
"StudentID": "54321",
"FirstName": "Jane",
"LastName": "Doe",
"Grade": 5
}
],
"SchoolName": "My Elementary School"
}
Подробнее здесь: https://stackoverflow.com/questions/754 ... ull-object