TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sujeet Raman
757
927
351.4k
Data Annotation error message is not displaying in postman response
Mar 29 2021 7:45 AM
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));
});
}
Reply
Answers (
2
)
Converting VB code to C#
HTML content to excel export and PDF in asp.net core 3.0