在.NET Core 1.0的全局级别(所有API响应)中,如何配置Startup.cs以便在JSON响应中删除/忽略空字段?
使用Newtonsoft.Json,您可以将以下属性应用于属性,但我希望避免将其添加到每个属性:
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string FieldName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string OtherName { get; set; }
Run Code Online (Sandbox Code Playgroud) 我正在准备从ASP.NET Core 2.2迁移到3.0。
As I don't use any more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json, I decided to see if I could drop the previous default Newtonsoft.Json. Do note, I'm aware that System.Text.Json will not completely replace Newtonsoft.Json.
I managed to do that everywhere, e.g.
var obj = JsonSerializer.Parse<T>(jsonstring);
var jsonstring = JsonSerializer.ToString(obj);
Run Code Online (Sandbox Code Playgroud)
but in one place, where I populate an existing object. …
c# asp.net-core razor-pages asp.net-core-3.0 system.text.json