2016-07-24 58 views
0

在我的Web API项目,我有范围数据标注工作不

[HttpPost] 
    public void Post([FromBody]AccountDTO accountDto) 

AccountDto包括一些属性,例如:

public int AccountId { get; protected set; } 
    [Range(0,100)] 
    public int AccountBalance { get; protected set; } 
    [RegularExpression(COMMA_SEPARATED_EMAILS_REGEX)] 
    public string Emails { get; set; } 

我还添加了过滤器捕捉模型误差:

public void OnActionExecuting(ActionExecutingContext context) 
    { 
     if (!context.ModelState.IsValid) 
     { 
      throw new InvalidDataException(string.Join(" ", context.ModelState.Keys.SelectMany(key => context.ModelState[key].Errors.Select(x => key + ": " + x.ErrorMessage)))); 
     } 
    } 

现在,如果电子邮件字符串不是有效的 - 它抛出预期的错误,也是我试过[EmailAddress], [Phone], [Required], [StringLength(3)],和所有工作正常。 只是[范围(0,100)不工作...

我试过后AccountDTO与accountBallance = 50033333,并没有抛出错误,也试过-5和我状态200

,我应该寻找?错误...感谢

回答

0

刚刚尝试这一点也许它帮助:

[Required(ErrorMessage = "AccountBalance is required")] 
[Range(1.00, 100.00, ErrorMessage = "AccountBalance must be between 1 and 100")] 
public Double AccountBalance { get; protected set; } 

,并确保您使用System.ComponentModel.DataAnnotations。

0

一旦我删除了protected验证工作。