Hi all,
i have web api 3.0 solution and i dont have any UI for that.All my response i have cheking in postman only .
For input validation i have used data annotation but i couldnt see the error message in post man.please help me what mistake i have done.
currently i am getting the error message like:
{
"studentid": [
"Error converting value {null} to type 'System.Int32'. Path 'studentid', line 2, position 26."
]
}
but i want to display the error message like:
- [Required (ErrorMessage = "student Id is required.This cannot be null or blank")]
- public int StudentId { get; set; }
What i have done so far:
1. my model
- public class Employee
- {
- [Required (ErrorMessage = "student Id is required.This cannot be null or blank")]
- public int StudentId { get; set; }
- [Required (ErrorMessage = "Student name is required.This cannot be null or blank")]
- [StringLength(100, MinimumLength = 2)]
- public string FirstName { get; set; }
- [Required]
- public string LastName { get; set; }
- [Required (ErrorMessage = "Student age is required.This cannot be null or blank")]
- public int Age{ get; set; }
- }
2. my controller
- [HttpPost("api/Student/Check")]
- public async Task Check([FromBody] students input)
- {
- if (!ModelState.IsValid)
- {
- returnBadRequest(Modelstate);
- }
- else {
- returnOk();
- }
- }
3. created one class for ValidatorActionFilter.cs
- public class ValidatorActionFilter : IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- if (!filterContext.ModelState.IsValid)
- {
- filterContext.Result = new BadRequestObjectResult(filterContext.ModelState);
- }
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- }
- }
startup.cs
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc(options =>
- {
- options.Filters.Add(typeof(ValidatorActionFilter));
- });
- }