.NET Core Razor page/MVC 返回json忽略空属性
2024-10-13
19
.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值了。
赞一波!
相关文章
- MVC5 Razor语法
- 【说站】php数组转json字符串
- 【说站】python json保存数据的方法
- ASP.NET Core 2.1中的ActionResult<T>
- .NET Core 3.0创建一个单独的可执行文件
- 在ASP.NET Core 2.1中使用HttpClientFactory的3种方法
- .NET Core MVC应用程序创建教程
- .NET Core中的File和PhysicalFile
- .NET Core 读取配置文件方法
- .NET Core 管道模型中间件及管道模拟实现
- ASP.NET Core多环境配置
- .NET Core 日志配置,NLog配置示例
- ASP.NET Core主机和应用启动流程
- .NET Core SkiaSharp替代System.Drawing的用法
- .NET Core 3.X路由中间件和终结点中间件
- .Net报错Could not find a part of the path bin\roslyn\csc.exe
- IIS中.NET Core报错HTTP Error 500.31 - ANCM Failed to Find Native Dependencies
- .NET Core分部视图PartialView用法
- .NET Core 视图组件Component用法
- ASP.NET Core的三种部署方式
文章评论
评论问答