mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 14:04:41 +08:00
增加自定义模态验证异常返回信息
This commit is contained in:
parent
d9a500b3b2
commit
75ceea1652
50
OpenAuth.WebApi/Model/CustomBadRequest.cs
Normal file
50
OpenAuth.WebApi/Model/CustomBadRequest.cs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
namespace OpenAuth.WebApi.Model
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 自定义模态验证异常返回信息
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -41,9 +41,7 @@ namespace OpenAuth.WebApi
|
|||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.Configure<ApiBehaviorOptions>(options => { options.SuppressModelStateInvalidFilter = true; });
|
//在startup中需要强制创建log4net
|
||||||
|
|
||||||
//在startup中需要强制创建log4net
|
|
||||||
var loggerFactory = LoggerFactory.Create(builder =>
|
var loggerFactory = LoggerFactory.Create(builder =>
|
||||||
{
|
{
|
||||||
builder.AddLog4Net();
|
builder.AddLog4Net();
|
||||||
@ -115,7 +113,15 @@ namespace OpenAuth.WebApi
|
|||||||
.ConfigureApiBehaviorOptions(options =>
|
.ConfigureApiBehaviorOptions(options =>
|
||||||
{
|
{
|
||||||
// 禁用自动模态验证
|
// 禁用自动模态验证
|
||||||
options.SuppressModelStateInvalidFilter = true;
|
// options.SuppressModelStateInvalidFilter = true;
|
||||||
|
|
||||||
|
//启动WebAPI自动模态验证,处理返回值
|
||||||
|
options.InvalidModelStateResponseFactory = context =>
|
||||||
|
{
|
||||||
|
var problems = new CustomBadRequest(context);
|
||||||
|
|
||||||
|
return new BadRequestObjectResult(problems);
|
||||||
|
};
|
||||||
}).AddNewtonsoftJson(options =>
|
}).AddNewtonsoftJson(options =>
|
||||||
{
|
{
|
||||||
//忽略循环引用
|
//忽略循环引用
|
||||||
|
Loading…
Reference in New Issue
Block a user