返回
.NET Core Razor page/MVC 返回json忽略空属性
2022-12-04 2766 0
.NET Core Razor page/MVC 返回json忽略空属性,修改program.cs。
添加配置
builder.Services.AddRazorPages()
.AddJsonOptions(options => {
options.JsonSerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull;
});
这个options里的DefaultIgnoreCondition枚举包含如下设置:
//
// 摘要:
// Controls how the System.Text.Json.Serialization.JsonIgnoreAttribute ignores properties
// on serialization and deserialization.
public enum JsonIgnoreCondition
{
//
// 摘要:
// Property will always be serialized and deserialized, regardless of System.Text.Json.JsonSerializerOptions.IgnoreNullValues
// configuration.
Never,
//
// 摘要:
// Property will always be ignored.
Always,
//
// 摘要:
// Property will only be ignored if it is null.
WhenWritingDefault,
//
// 摘要:
// If the value is null, the property is ignored during serialization. This is applied
// only to reference-type properties and fields.
WhenWritingNull
}这样就可以在输入Json的时候忽略null值了。
网友点评
提交
相关阅读
C# .NET Core中Microsoft.Extensions.Caching.Redis库用法
.NET Core在program.cs里GetService获取服务实例
.NET Core NLog日志存入数据库配置
.NET Core里UseStatusCodePagesWithReExecute和UseStatusCodePagesWithRedirects的区别
.NET Core 图像处理:Magick.NET 与 SkiaSharp 的全面对比
.NET Core本地调试101 Switching Protocols是什么请求?
.NET Core MVC 获取UrlReferer
MVC5 Razor语法