netcore默认有Newtonsoft.Json的引用,当我们直接在接口的出参参数添加JsonIgnore的时候并不起作用。
public class A { [JsonIgnore] public int OpenValInt =>0; }
这里需要做两个配置,首先在nuget包安装Microsoft.AspNetCore.Mvc.NewtonsoftJson包:
第二步,在startup的ConfigureServices做如下配置:
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews().AddNewtonsoftJson(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); }
然后再运行,看下效果。