我几乎总是希望在提交后检查ModelSate.IsValid
是否被调用。每次提交开始时都需要检查,这违反了DRY原则,有没有一种自动检查的方法?
例子:
[HttpPost("RegisterUser")]
[AllowAnonymous]
public async Task<IActionResult> RegisterUser([FromBody] UserRegisterViewModel vmodel)
{
if(!ModelState.IsValid) // This code is repeated at every postback
return ModelInvalidAction(); // Is there a way to avoid having to write it down?
// do other things
return StatusCode(201);
}