1 services.AddMvc(options => 2 { 3 options.Filters.Add<ApiExceptionAttribute>(); 4 }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1) 5 .AddJsonOptions(options => 6 { 7 options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();//JSON首字母小写解决 8 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; 9 //返回Json属性命名默认按照小驼峰规则 10 options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 11 });
1 //参数验证 2 services.Configure<ApiBehaviorOptions>(options => 3 { 4 options.InvalidModelStateResponseFactory = (context) => 5 { 6 var error = context.ModelState.GetValidationSummary(); 7 8 return new JsonResult(Result.FromError($"参数验证不通过:{error.ToString()}", ResultCode.InvalidParams)); 9 }; 10 });