c# - 对 ASP.NET Core 中缺少所需属性的响应
问题描述
给定以下控制器:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// POST api/values
[HttpPost]
public ActionResult<string> Post([FromBody] Model req)
{
return $"Your name is {req.Name}";
}
}
public class Model
{
[Required] public string Name { get; set; }
}
}
如果我发布一个空的 body {}
,响应是:
{
"errors": {
"Name": [
"The Name field is required."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "80000002-0002-ff00-b63f-84710c7967bb"
}
我想更改此响应,以便将错误消息自动传递给用户变得更容易。所以我希望它看起来更像这样:
{
"error": 999,
"message": "Field 'name' is required."
}
我试图RequiredAttribute
像这样扩展 -class:
public class MyRequiredAttribute : RequiredAttribute
{
public MyRequiredAttribute()
{
ErrorMessage = "{0} is required";
}
}
遗憾的是,它只会更改集合中返回的字符串,就像这样
{
"errors": {
"Name": [
"Name is required"
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "80000006-0000-ff00-b63f-84710c7967bb"
}
解决方案
在使用应用了 ApiController 属性的控制器时,ASP.NET Core 通过返回 400 Bad Request 并以 ModelState 作为响应正文来自动处理模型验证错误。它与自动 HTTP 400 响应有关。您可以自定义 BadRequest 响应,如下所示:
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
var modelState = actionContext.ModelState;
return new BadRequestObjectResult(FormatOutput(modelState));
};
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
根据您的心血来潮定制FormatOutput
方法。
public List<Base> FormatOutput(ModelStateDictionary input)
{
List<Base> baseResult = new List<Base>();
foreach (var modelState in input.Values)
{
foreach (ModelError error in modelState.Errors)
{
Base basedata = new Base();
basedata.Error = StatusCodes.Status400BadRequest;
basedata.Message =error.ErrorMessage;
baseResult.Add(basedata);
}
}
return baseResult;
}
public class Base
{
public int Error { get; set; }
public string Message { get; set; }
}
推荐阅读
- android - 带有与旧 Android 版本兼容的文件提供程序的清单
- python - 来自archive.ics的“成人数据集”(数据集)的机器学习...... KNeighborsClassifier不会运行
- c - 如何在 C 语言中填补文本之间的空白
- kubernetes - Kubernetes 中的 Zuul 部署
- css - 如何在网站徽标上更改翻转状态
- c - 如何防止字符串读取 C 中的 CTF 标志
- javascript - 如何声明一个 3D 数组并单步执行?
- javascript - 如何将 javascript 客户端连接到 ams(rtmp 和共享对象)
- python - 子进程在后台运行并将输出逐行写入文件
- mongodb - 仅获取那些没有匹配总和的记录