title: Custom Model Validator for Integer value in ASP.NET Core Web API
body: <p>I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: </p>
  
  <pre><code>public class ValidateIntegerValueAttribute : ValidationAttribute
      {
          protected override ValidationResult IsValid(object value, ValidationContext validationContext)
          {
              if (value != null)
              {
                  int output;
  
                  var isInteger = int.TryParse(value.ToString(), out output);
  
                  if (!isInteger)
                  {
                      return new ValidationResult("Must be a Integer number");
                  }
              }
  
              return ValidationResult.Success;
          }
      }
  </code></pre>
  
  <p>I have also an Filter class for model validation globally in application request pipeline. Here is my code:</p>
  
  <pre><code>public class MyModelValidatorFilter: IActionFilter
  {   
      public void OnActionExecuting(ActionExecutingContext context)
      {
          if (context.ModelState.IsValid)
              return;
  
          var errors = new Dictionary&lt;string, string[]&gt;();
  
          foreach (var err in actionContext.ModelState)
          {
              var itemErrors = new List&lt;string&gt;();
  
              foreach (var error in err.Value.Errors){
                  itemErrors.Add(error.Exception.Message);
              }
  
              errors.Add(err.Key, itemErrors.ToArray());
          }
  
          actionContext.Result = new OkObjectResult(new MyResponse
          {
              Errors = errors
          });
      }
  }
  </code></pre>
  
  <p>The model class with validation is below:</p>
  
  <pre><code>public class MyModelClass
  </code></pre>
  
  <p>{       </p>
  
  <pre><code>[ValidateIntegerValue(ErrorMessage = "{0} must be a Integer Value")]
  [Required(ErrorMessage = "{0} is required")]
  public int Level { get; set; }        
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone please let me know why the attribute integer validation class is not working.</p>