From 75ceea1652536457235e5a80fc3d2bd43bde287f Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sun, 9 May 2021 21:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=A8=A1=E6=80=81=E9=AA=8C=E8=AF=81=E5=BC=82=E5=B8=B8=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.WebApi/Model/CustomBadRequest.cs | 50 +++++++++++++++++++++++ OpenAuth.WebApi/Startup.cs | 14 +++++-- 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 OpenAuth.WebApi/Model/CustomBadRequest.cs diff --git a/OpenAuth.WebApi/Model/CustomBadRequest.cs b/OpenAuth.WebApi/Model/CustomBadRequest.cs new file mode 100644 index 00000000..a0ede947 --- /dev/null +++ b/OpenAuth.WebApi/Model/CustomBadRequest.cs @@ -0,0 +1,50 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace OpenAuth.WebApi.Model +{ + /// + /// 自定义模态验证异常返回信息 + /// + public class CustomBadRequest : ValidationProblemDetails + { + + public CustomBadRequest(ActionContext context) + { + Title = "WebAPI客户端传入的参数无效"; + Detail = "vue客户端(或其他方式)调用WebAPI时传入的参数类型与接口需要的类型不匹配"; + Status = 400; + ConstructErrorMessages(context); + } + private void ConstructErrorMessages(ActionContext context) + { + foreach (var keyModelStatePair in context.ModelState) + { + var key = keyModelStatePair.Key; + var errors = keyModelStatePair.Value.Errors; + if(errors.Count == 0) continue; + + if (errors.Count == 1) + { + var errorMessage = GetErrorMessage(errors[0]); + Errors.Add(key, new[] { errorMessage }); + } + else + { + var errorMessages = new string[errors.Count]; + for (var i = 0; i < errors.Count; i++) + { + errorMessages[i] = GetErrorMessage(errors[i]); + } + Errors.Add(key, errorMessages); + } + + } + } + string GetErrorMessage(ModelError error) + { + return string.IsNullOrEmpty(error.ErrorMessage) ?"The input was not valid.":error.ErrorMessage; + } + } +} diff --git a/OpenAuth.WebApi/Startup.cs b/OpenAuth.WebApi/Startup.cs index 79a73ac5..b135c2be 100644 --- a/OpenAuth.WebApi/Startup.cs +++ b/OpenAuth.WebApi/Startup.cs @@ -41,9 +41,7 @@ namespace OpenAuth.WebApi // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.Configure(options => { options.SuppressModelStateInvalidFilter = true; }); - - //在startup中需要强制创建log4net + //在startup中需要强制创建log4net var loggerFactory = LoggerFactory.Create(builder => { builder.AddLog4Net(); @@ -115,7 +113,15 @@ namespace OpenAuth.WebApi .ConfigureApiBehaviorOptions(options => { // 禁用自动模态验证 - options.SuppressModelStateInvalidFilter = true; + // options.SuppressModelStateInvalidFilter = true; + + //启动WebAPI自动模态验证,处理返回值 + options.InvalidModelStateResponseFactory = context => + { + var problems = new CustomBadRequest(context); + + return new BadRequestObjectResult(problems); + }; }).AddNewtonsoftJson(options => { //忽略循环引用