asp.net core model 验证和取出 ErrorMessage 信息

2018-01-11 12:47:39来源:oschina作者:Contac人点击

分享
public class Users
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "邮箱不能为空")]
[EmailAddressAttribute(ErrorMessage = "邮箱格式不正确")]
public string Email { get; set; }
[DisplayName("密码")]
[Required(ErrorMessage = "密码不能为空")]
public string Password { get; set; }
}

验证


[HttpPost]
public IActionResult Get([FromRoute]Users model)
{
if (!ModelState.IsValid)
{
var sb = new StringBuilder();
foreach (var key in ModelState.Keys)
{
var state = ModelState[key];
if (state.Errors.Any())
{
sb.Append(state.Errors.First().ErrorMessage + "/n/r");
}
}
return Ok(sb.ToString());
}
return Ok(model);
}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台