Sujeet Raman

Sujeet Raman

  • 857
  • 927
  • 352.1k

how to validate model in asp.net core 3.1 web api post method?

Mar 26 2021 7:11 AM
Hi
 
I have an api post method. I don't have any UI to use that api so i need to show my error message in postman. If I miss one property the missing property error message should be displayed in postman.i confused with many post in internet.
 
my post method
  1. [HttpPost("api/Student/Check")]  
  2. public async Task Check([FromBody] students input)  
  3. {  
  4. var result = await _students.studentAsync(input);  
  5. return Ok(result);  
  6. }  
  7. my model  
  8. public class students  
  9. {  
  10. [JsonProperty("studentid")]  
  11. public int studentid{ getset; }  
  12. [JsonProperty("studentname")]  
  13. public int studentname{ getset; }  
  14. [JsonProperty("studentage")]  
  15. public int studentage{ getset; }  
  16. }  
  17. }  
I should get like:
 
Expected Result: Status code is 400 bad request and response should be as below:
 
student id is required.This cannot be null or blank.
student name is required.This cannot be null or blank
student age field is required.This cannot be null or blank.

Answers (5)