Part 4 - How to post data with validation in the ASP.NET Web API using Http Client

Part 4 - Post data to Web API using HTTP Client (in MVC client application)
Part 4 - How to post data with validation in the ASP.NET Web API using Http Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DAL;

namespace DotnetAwesome.Api.Controllers
{
    public class ExampleController : ApiController
    {
        public HttpResponseMessage Get()
        {
            List<Demo_Employees> allEmp = new List<Demo_Employees>();
            using (MyDatabaseEntities dc = new MyDatabaseEntities()) //here MyDatabaseEntities is our datacontext
            {
                allEmp = dc.Demo_Employees.OrderBy(a => a.EmployeeID).ToList(); // I have added linq code for fetch data, you can use Sql client for fetch data
                HttpResponseMessage response;
                response = Request.CreateResponse(HttpStatusCode.OK, allEmp);
                return response;
            }
        }
        public HttpResponseMessage Post(Demo_Employees emp)
        {
            HttpResponseMessage response;
            if (ModelState.IsValid)
            {
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    dc.Demo_Employees.Add(emp);
                    dc.SaveChanges();
                }
                response = Request.CreateResponse(HttpStatusCode.Created, emp);
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, "Error! Please try again with valid data.");
            }
            return response;
        }
    }
}
Posted By :