【发布时间】:2021-09-13 16:49:59
【问题描述】:
目前我正在使用 ActionFilterAttribute 来验证模型。如果我在字符串字段中给出一个整数值,我将面临这个问题,它将自动转换为字符串而不是抛出错误。 例如:
{
"Id":1,
"employeeName":1
}
在上述模型类中employeeName的数据类型是一个字符串。
模型验证器:
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
var message = string.Join("; ", context.ModelState.Values
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage));
context.Result = new BadRequestObjectResult(message);
}
}
我想抛出一个错误,比如 value must be a string.
【问题讨论】:
标签: .net-core asp.net-core-webapi