0

I am doing a validation task for my Web API project. It exposes APIs to the front-end. The front-end should input valid values in the json file, but sometimes, if the invalid values coming it cause exceptions in my project.

For example, someId cannot be 0 or other invalid values. Because if 0 was set to someId, the DB will yell the foreign key constraints error.

My solution to this is using the attribute validation

 [Range(1, 10, ErrorMessage = "{0} Values must be in range {1} to {2}")]
 public int? someId { get; set; }

So it will prevent the invalid values from UI. But the little hassle is I need to hard code the valid data range in each fields. So I am asking if it has a better solution than mine?

leon365
  • 127
  • 2
  • 8
  • This post provides some reference to the problem. https://stackoverflow.com/questions/9296744/changing-validation-range-programmatically-mvc3-asp-net – leon365 Jun 25 '19 at 16:45

0 Answers0