須要在序列化時候,忽略掉某些字段,對Newtonsoft.Json進行全局配置,以下:ide
1. 自定 ContractResolverorm
public class MyContractResolver : DefaultContractResolver { protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { if (!typeof(EntityBase).IsAssignableFrom(type)) { return base.CreateProperties(type, memberSerialization); } IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); foreach (var prop in props) { if (prop.DeclaringType.FullName != null && prop.DeclaringType.FullName.StartsWith("PWMIS.DataMap.Entity.EntityBase")) { prop.Ignored = true; } } return props; } }
2. 應用程序啓動的時候對JsonConvert進行默認配置:blog
JsonSerializerSettings setting = new JsonSerializerSettings() { Formatting = Formatting.None, ContractResolver = new MyContractResolver() }; JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => { //日期類型默認格式化處理 setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //空值處理 setting.NullValueHandling = NullValueHandling.Ignore; return setting; });