修复json序列化配置忽略属性的问题
This commit is contained in:
@@ -276,8 +276,8 @@ namespace COMCMS.Web.Areas.AdminCP.Controllers
|
|||||||
|
|
||||||
IList<Article> list = Article.FindAll(ex, Article._.Sequence.Asc().And(Article._.Id.Desc()), null, startRowIndex, numPerPage);
|
IList<Article> 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);
|
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 Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { total = totalCount, rows = list }), "text/plain");
|
||||||
//return Json(new { total = totalCount, rows = list }, JsonRequestBehavior.AllowGet);
|
return Json(new { total = totalCount, rows = list });
|
||||||
}
|
}
|
||||||
|
|
||||||
//添加文章
|
//添加文章
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 限制某些属性输出或排除,设置为保留时,只输出保留属性,其他属性全部忽略。
|
|
||||||
/// 设置为排除,只排除设置的属性
|
|
||||||
/// </summary>
|
|
||||||
public class LimitPropsContractResolver : CamelCasePropertyNamesContractResolver
|
|
||||||
{
|
|
||||||
private readonly string[] _props = null;
|
|
||||||
|
|
||||||
private readonly bool _retain;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
/// <summary>
|
|
||||||
/// 构造函数
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="props">传入的属性数组</param>
|
|
||||||
/// <param name="retain">true:表示props是需要保留的字段 false:表示props是要排除的字段</param>
|
|
||||||
public LimitPropsContractResolver(string[] props, bool retain = true)
|
|
||||||
{
|
|
||||||
//指定要序列化属性的清单
|
|
||||||
this._props = props;
|
|
||||||
|
|
||||||
this._retain = retain;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override IList<JsonProperty> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -75,8 +75,12 @@ namespace COMCMS.Web
|
|||||||
services.Configure<SystemSetting>(Configuration.GetSection("SystemSetting"));
|
services.Configure<SystemSetting>(Configuration.GetSection("SystemSetting"));
|
||||||
|
|
||||||
services
|
services
|
||||||
.AddMvc()
|
.AddMvc(options =>
|
||||||
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
|
{
|
||||||
|
//记录错误
|
||||||
|
options.Filters.Add<HttpGlobalExceptionFilter>();
|
||||||
|
})
|
||||||
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
|
||||||
.AddJsonOptions(JsonOptionsConfig.ConfigJsonOptions);
|
.AddJsonOptions(JsonOptionsConfig.ConfigJsonOptions);
|
||||||
|
|
||||||
//防止汉字被自动编码
|
//防止汉字被自动编码
|
||||||
@@ -84,11 +88,7 @@ namespace COMCMS.Web
|
|||||||
{
|
{
|
||||||
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
|
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
|
||||||
});
|
});
|
||||||
//记录错误
|
|
||||||
services.AddMvc(options =>
|
|
||||||
{
|
|
||||||
options.Filters.Add<HttpGlobalExceptionFilter>();
|
|
||||||
});
|
|
||||||
services.AddSenparcGlobalServices(Configuration)//Senparc.CO2NET 全局注册
|
services.AddSenparcGlobalServices(Configuration)//Senparc.CO2NET 全局注册
|
||||||
.AddSenparcWeixinServices(Configuration);//Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加)
|
.AddSenparcWeixinServices(Configuration);//Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user