how to check Validation in ASP.NET Web API

Dear all,
how can i check validaion on data dictionary attributes for example check if Firstname and lastname is not null or length>0 using Model Validation in ASP.NET Web API
public HttpResponseMessage Post([FromBody] List<Dictionary<string,string>> _lstDicobject)
      {
//JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(kvpList)) object (person) data type 
return JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(kvpList)).sendMail();
     }

Open in new window

where person class
class person {
public string Firstname {set ; get;}
public string lastname {set ; get;}
public string send mail()
{
return Firstname+" "+ lastname; 
}
}

Open in new window

wael_shehabAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

Use DataAnnodations on your Model class.

       
class person
{
     [Required]
     public string Firstname {set ; get;}
     
     [Required]
     public string lastname {set ; get;}
    
     public string send mail()
     {
             return Firstname+" "+ lastname; 
      }
}

Open in new window



public HttpResponseMessage Post([FromBody] List<Dictionary<string,string>> _lstDicobject)
{
      if (ModelState.IsValid)
      {
          return JsonConvert.DeserializeObject<Person>(JsonConvert.SerializeObject(kvpList)).sendMail();
      }
    else
     {
           return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
}

Open in new window



http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
wael_shehabAuthor Commented:
thank you for your reply,
yes i  tried to use this code and it is  not working because i tried to send data dictionary with out sending last name ex(var ObjContentData = { 'Firstname ': 'WAEL')  and it is accepted it (ModelState.IsValid=true)always so how to check validation
, so any idea
wael_shehabAuthor Commented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wael_shehabAuthor Commented:
this solution, i have found is  help me to solve my problem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.