From 1f186cccd5cea7324c60a4071703f5751d2e09cb Mon Sep 17 00:00:00 2001 From: xxred Date: Fri, 29 Mar 2019 16:25:44 +0800 Subject: [PATCH] =?UTF-8?q?json=E5=BA=8F=E5=88=97=E5=8C=96=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E6=AE=B5=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COMCMS.Web/COMCMS.Web.csproj | 1 + COMCMS.Web/Configuration/JsonOptionsConfig.cs | 82 +++++++++++++++++++ COMCMS.Web/Startup.cs | 7 +- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 COMCMS.Web/Configuration/JsonOptionsConfig.cs diff --git a/COMCMS.Web/COMCMS.Web.csproj b/COMCMS.Web/COMCMS.Web.csproj index 494a446..d6023f9 100644 --- a/COMCMS.Web/COMCMS.Web.csproj +++ b/COMCMS.Web/COMCMS.Web.csproj @@ -32,6 +32,7 @@ + diff --git a/COMCMS.Web/Configuration/JsonOptionsConfig.cs b/COMCMS.Web/Configuration/JsonOptionsConfig.cs new file mode 100644 index 0000000..94b1a6c --- /dev/null +++ b/COMCMS.Web/Configuration/JsonOptionsConfig.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using XCode; + +namespace COMCMS.Web.Configuration +{ + public class JsonOptionsConfig + { + public static void ConfigJsonOptions(MvcJsonOptions options) + { + var settings = options.SerializerSettings; + //日期类型默认格式化处理 + // //setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; + settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; + + //// 空值处理 + //setting.NullValueHandling = NullValueHandling.Ignore; + + // 最大序列化深度,包括请求传参的序列化深度,不可设置太小了 + settings.MaxDepth = 5; + + // 忽略循环引用 + settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + + // 全局设置要排除的字段 + settings.ContractResolver = new LimitPropsContractResolver(new[] { "password", "extends" }, false); + } + } + + /// + /// 限制某些属性输出或排除,设置为保留时,只输出保留属性,其他属性全部忽略。 + /// 设置为排除,只排除设置的属性 + /// + public class LimitPropsContractResolver : CamelCasePropertyNamesContractResolver// DefaultContractResolver + { + private readonly string[] _props = null; + + private readonly bool _retain; + + /// + /// + /// 构造函数 + /// + /// 传入的属性数组 + /// true:表示props是需要保留的字段 false:表示props是要排除的字段 + public LimitPropsContractResolver(string[] props, bool retain = false) + { + //指定要序列化属性的清单 + this._props = props; + + this._retain = retain; + } + + protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + { + var list = base.CreateProperties(type, memberSerialization); + + // 非实体类型直接返回 + if (!typeof(EntityBase).IsAssignableFrom(type)) + { + return list; + } + + //只保留清单有列出的属性 + list = list.Where(p => + { + var has = _props.Any(a => a.Equals(p.PropertyName, StringComparison.OrdinalIgnoreCase)); + if (_retain) + { + return has; + } + + return !has; + }).ToList(); + return list; + } + } +} diff --git a/COMCMS.Web/Startup.cs b/COMCMS.Web/Startup.cs index 4451f8d..823c34f 100644 --- a/COMCMS.Web/Startup.cs +++ b/COMCMS.Web/Startup.cs @@ -23,6 +23,7 @@ using Senparc.CO2NET.RegisterServices; using Senparc.Weixin; using Senparc.CO2NET.Cache; using System.Configuration; +using COMCMS.Web.Configuration; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.Options; @@ -73,7 +74,11 @@ namespace COMCMS.Web //部分系统配置 services.Configure(Configuration.GetSection("SystemSetting")); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ; + services + .AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) + .AddJsonOptions(JsonOptionsConfig.ConfigJsonOptions); + //防止汉字被自动编码 services.Configure(options => {