From f91ef8d4768cba76b8ceec58b1d6a3c0bf67149c Mon Sep 17 00:00:00 2001 From: "DESKTOP-M4V1LH5\\Jin" Date: Fri, 29 Mar 2019 15:36:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COMCMS.Common/COMCMS.Common.csproj | 2 +- COMCMS.Core/COMCMS.Core.csproj | 2 +- COMCMS.Web/COMCMS.Web.csproj | 2 +- COMCMS.Web/Common/Configuration.cs | 85 +++++++++++++++++++++++++++ COMCMS.Web/Startup.cs | 2 +- XCoder/XCoder(代码生成器).csproj | 8 +-- XCoder/packages.config | 4 +- 7 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 COMCMS.Web/Common/Configuration.cs diff --git a/COMCMS.Common/COMCMS.Common.csproj b/COMCMS.Common/COMCMS.Common.csproj index f191c4c..2094606 100644 --- a/COMCMS.Common/COMCMS.Common.csproj +++ b/COMCMS.Common/COMCMS.Common.csproj @@ -6,7 +6,7 @@ - + diff --git a/COMCMS.Core/COMCMS.Core.csproj b/COMCMS.Core/COMCMS.Core.csproj index ebcb1b6..3d5e990 100644 --- a/COMCMS.Core/COMCMS.Core.csproj +++ b/COMCMS.Core/COMCMS.Core.csproj @@ -5,7 +5,7 @@ - + diff --git a/COMCMS.Web/COMCMS.Web.csproj b/COMCMS.Web/COMCMS.Web.csproj index e5989f5..2d31173 100644 --- a/COMCMS.Web/COMCMS.Web.csproj +++ b/COMCMS.Web/COMCMS.Web.csproj @@ -10,7 +10,7 @@ - + diff --git a/COMCMS.Web/Common/Configuration.cs b/COMCMS.Web/Common/Configuration.cs new file mode 100644 index 0000000..3d1bd27 --- /dev/null +++ b/COMCMS.Web/Common/Configuration.cs @@ -0,0 +1,85 @@ +using Microsoft.AspNetCore.Mvc; +using NewLife.Serialization; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using XCode; + +namespace COMCMS.Web.Common +{ + 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 + { + private readonly string[] _props = null; + + private readonly bool _retain; + + /// + /// + /// 构造函数 + /// + /// 传入的属性数组 + /// true:表示props是需要保留的字段 false:表示props是要排除的字段 + public LimitPropsContractResolver(string[] props, bool retain = true) + { + //指定要序列化属性的清单 + 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(); + NewLife.Log.XTrace.WriteLine(list.ToJson()); + return list; + } + } +} diff --git a/COMCMS.Web/Startup.cs b/COMCMS.Web/Startup.cs index 68b9df2..24da688 100644 --- a/COMCMS.Web/Startup.cs +++ b/COMCMS.Web/Startup.cs @@ -73,7 +73,7 @@ namespace COMCMS.Web //部分系统配置 services.Configure(Configuration.GetSection("SystemSetting")); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); ; + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(JsonOptionsConfig.ConfigJsonOptions); //防止汉字被自动编码 services.Configure(options => { diff --git a/XCoder/XCoder(代码生成器).csproj b/XCoder/XCoder(代码生成器).csproj index da2b1c2..7d57095 100644 --- a/XCoder/XCoder(代码生成器).csproj +++ b/XCoder/XCoder(代码生成器).csproj @@ -30,8 +30,8 @@ 4 - - ..\packages\NewLife.Core.8.1.2019.324\lib\net45\NewLife.Core.dll + + ..\packages\NewLife.Core.8.1.2019.327\lib\net45\NewLife.Core.dll @@ -46,8 +46,8 @@ - - ..\packages\NewLife.XCode.9.11.2019.324\lib\net45\XCode.dll + + ..\packages\NewLife.XCode.9.11.2019.327\lib\net45\XCode.dll diff --git a/XCoder/packages.config b/XCoder/packages.config index f56c6dd..a39aeb2 100644 --- a/XCoder/packages.config +++ b/XCoder/packages.config @@ -1,5 +1,5 @@  - - + + \ No newline at end of file