2016-07-24 47 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驗證工作。