英文:
Minimal API Results.Ok Not Returning Full Object
问题
在进行较大的Minimal API项目时,Results.Ok()没有返回完整的对象。但是,切换到Results.Text()会返回完整的对象。
我已经包含了一个简单应用程序的完整代码清单,以查看是否可以复现这种行为。该应用程序包括两个端点,应该返回相同的对象。
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<Student> Students = new List<Student>();
}
public class Student
{
public string StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Grade { get; set; }
}
使用Postman,我得到以下结果。
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"
}
这是否是预期行为?在使用Results.Ok()时,是否有什么我忽略的内容?
英文:
While working on a larger Minimal API project, Results.Ok() was not returning the full object. However, switching to Results.Text() returns the full object.
I've included the full listing of a simple application that I created to see if I can reproduce the behavior. The application includes two endpoints that are supposed to return the same object.
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<Student> Students = new List<Student>();
}
public class Student
{
public string StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Grade { get; set; }
}
Using Postman I get the following results.
https://localhost:7000/SchoolsV1
{
"schoolName": "My Elementary School"
}
Then
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"
}
Is this expected behavior? Is there something I'm missing when using Results.Ok()?
答案1
得分: 2
Results.Ok
(实际上在这里不需要,只需使用app.MapGet("/SchoolsV1", () => school)
)使用默认的System.Text.Json
序列化程序(有关更多信息,请参阅文档),默认情况下不序列化字段。有几种选项。
最简单的选项是将School.Students
更改为属性:
public class School
{
public string SchoolName { get; set; }
public List<Student> Students { get; set; } = new List<Student>();
}
或者配置Microsoft.AspNetCore.Http.Json.JsonOptions
:
builder.Services.Configure<JsonOptions>(opts => opts.SerializerOptions.IncludeFields = true);
var app = builder.Build();
// ...
英文:
Results.Ok
(which is actually not needed here, you can use just app.MapGet("/SchoolsV1", () => school
) uses the default System.Text.Json
serializer (more info about it can be found in the docs) which does not serialize fields by default. There are several options.
Easiest one is to change School.Students
to be a property:
public class School
{
public string SchoolName { get; set; }
public List<Student> Students { get; set; } = new List<Student>();
}
Or configure Microsoft.AspNetCore.Http.Json.JsonOptions
:
builder.Services.Configure<JsonOptions>(opts => opts.SerializerOptions.IncludeFields = true);
var app = builder.Build();
// ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论