【发布时间】:2018-12-15 12:57:21
【问题描述】:
我需要从“ModelState”中捕获错误以发送个性化消息。问题是如果 UserDTO 的属性具有“必需”属性,则永远不会执行过滤器。如果去掉就输入过滤器,但是modelState有效
[HttpPost]
[ModelState]
public IActionResult Post([FromBody] UserDTO currentUser)
{
/*if (!ModelState.IsValid)
{
return BadRequest();
}*/
return Ok();
}
public class ModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext currentContext)
{
if (!currentContext.ModelState.IsValid)
{
currentContext.Result = new ContentResult
{
Content = "Modelstate not valid",
StatusCode = 400
};
}
else
{
base.OnActionExecuting(currentContext);
}
}
}
public class UserDTO
{
[Required]
public string ID { get; set; }
public string Name { get; set; }
}
【问题讨论】:
标签: c# asp.net-core-2.0 asp.net-core-webapi asp.net-core-2.1