Minimal API Results.Ok 不返回完整对象

huangapple go评论77阅读模式
英文:

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 = &quot;My Elementary School&quot;;
school.Students.Add(new Student() { FirstName = &quot;Bill&quot;, LastName = &quot;Smith&quot;, Grade = 4, StudentID = &quot;123456&quot; });
school.Students.Add(new Student() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot;, Grade = 5, StudentID = &quot;54321&quot; });

app.MapGet(&quot;/SchoolsV1&quot;, () =&gt;
{
    return Results.Ok(school);
});

app.MapGet(&quot;/SchoolsV2&quot;, () =&gt;
{
    string strValue = JsonConvert.SerializeObject(school, Formatting.Indented);
    return Results.Text(strValue, &quot;application/json&quot;, null);
});

app.Run();


public class School
{
    public string SchoolName { get; set; }
    public List&lt;Student&gt; Students = new List&lt;Student&gt;();
}
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

{
    &quot;schoolName&quot;: &quot;My Elementary School&quot;
}

Then

https://localhost:7000/SchoolsV2


{
    &quot;Students&quot;: [
        {
            &quot;StudentID&quot;: &quot;123456&quot;,
            &quot;FirstName&quot;: &quot;Bill&quot;,
            &quot;LastName&quot;: &quot;Smith&quot;,
            &quot;Grade&quot;: 4
        },
        {
            &quot;StudentID&quot;: &quot;54321&quot;,
            &quot;FirstName&quot;: &quot;Jane&quot;,
            &quot;LastName&quot;: &quot;Doe&quot;,
            &quot;Grade&quot;: 5
        }
    ],
    &quot;SchoolName&quot;: &quot;My Elementary School&quot;
}

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(&quot;/SchoolsV1&quot;, () =&gt; 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&lt;Student&gt; Students { get; set; } = new List&lt;Student&gt;();
}

Or configure Microsoft.AspNetCore.Http.Json.JsonOptions:

builder.Services.Configure&lt;JsonOptions&gt;(opts =&gt; opts.SerializerOptions.IncludeFields = true);
var app = builder.Build();
// ...

huangapple
  • 本文由 发表于 2023年2月19日 02:30:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定