From f974b22d65bf9b9dad9167eb8b4cc081c9be693a Mon Sep 17 00:00:00 2001 From: xxred Date: Fri, 29 Mar 2019 22:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djson=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E9=85=8D=E7=BD=AE=E5=BF=BD=E7=95=A5=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdminCP/Controllers/ArticleController.cs | 4 +- COMCMS.Web/Common/Configuration.cs | 85 ------------------- COMCMS.Web/Startup.cs | 20 ++--- 3 files changed, 12 insertions(+), 97 deletions(-) delete mode 100644 COMCMS.Web/Common/Configuration.cs diff --git a/COMCMS.Web/Areas/AdminCP/Controllers/ArticleController.cs b/COMCMS.Web/Areas/AdminCP/Controllers/ArticleController.cs index af11af0..4c6f9d5 100644 --- a/COMCMS.Web/Areas/AdminCP/Controllers/ArticleController.cs +++ b/COMCMS.Web/Areas/AdminCP/Controllers/ArticleController.cs @@ -276,8 +276,8 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers IList
list = Article.FindAll(ex, Article._.Sequence.Asc().And(Article._.Id.Desc()), null, startRowIndex, numPerPage); long totalCount = Article.FindCount(ex, Article._.Sequence.Asc().And(Article._.Id.Desc()), null, startRowIndex, numPerPage); - return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { total = totalCount, rows = list }), "text/plain"); - //return Json(new { total = totalCount, rows = list }, JsonRequestBehavior.AllowGet); + //return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { total = totalCount, rows = list }), "text/plain"); + return Json(new { total = totalCount, rows = list }); } //添加文章 diff --git a/COMCMS.Web/Common/Configuration.cs b/COMCMS.Web/Common/Configuration.cs deleted file mode 100644 index 3d1bd27..0000000 --- a/COMCMS.Web/Common/Configuration.cs +++ /dev/null @@ -1,85 +0,0 @@ -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 823c34f..e3148f2 100644 --- a/COMCMS.Web/Startup.cs +++ b/COMCMS.Web/Startup.cs @@ -69,14 +69,18 @@ namespace COMCMS.Web { options.IdleTimeout = TimeSpan.FromSeconds(120); options.Cookie.HttpOnly = true; - + }); //部分系统配置 services.Configure(Configuration.GetSection("SystemSetting")); services - .AddMvc() - .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) + .AddMvc(options => + { + //记录错误 + options.Filters.Add(); + }) + .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddJsonOptions(JsonOptionsConfig.ConfigJsonOptions); //防止汉字被自动编码 @@ -84,11 +88,7 @@ namespace COMCMS.Web { options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All); }); - //记录错误 - services.AddMvc(options => - { - options.Filters.Add(); - }); + services.AddSenparcGlobalServices(Configuration)//Senparc.CO2NET 全局注册 .AddSenparcWeixinServices(Configuration);//Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加) @@ -122,7 +122,7 @@ namespace COMCMS.Web app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } - + //其他错误页面处理 app.UseStatusCodePagesWithReExecute("/StatusCode/{0}"); @@ -148,7 +148,7 @@ namespace COMCMS.Web routes.MapRoute( name: "article", template: "{title}/index.html", - defaults:new { controller ="Home", action= "Article" } + defaults: new { controller = "Home", action = "Article" } ); routes.MapRoute( name: "article2",