Thursday, January 18, 2018

How to create Web api And consuming web api in asp.net mvc application using http client

How to create api service in asp.net mvc

public class Student
        {
            public int studentId{get;set;}
            public string studentName{set;get;}

       }
public class StudentController : ApiController
    {

List<Student> _studentList=new List<Student>(){new Student(){ studentId=1, studentName=”A”}, new Student(){ studentId=2, studentName=”B”} };
     
        [Route("api/methodname")]
        [AcceptVerbs("GET")]
        public HttpResponseMessage GET()
        {
            ResponseModel responseStatus;
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, _studentList);       
            }
            catch (Exception ex)
            {
            }
        }

        public HttpResponseMessage GET(int studentId)
        {
             

         Student student=_studentList.FirstOrDefault(x=>x.studentId==studentId);
            if(student!=null)
             return Request.CreateResponse(HttpStatusCode.OK,student);       
           else
             return Request.CreateResponse(HttpStatusCode. NotFound
, null);       


        }

        [Route("api/Edit")]
        [AcceptVerbs("PUT")]
        public HttpResponseMessage PUT(int studentId,[FromBody]Student studentDetails)
        {
           
              Student student=_studentList.FirstOrDefault(x=>x.studentId==studentId);
            if(student!=null)
{
      student.studentName= studentDetails.studentName;

return Request.CreateResponse(HttpStatusCode.OK,student);       
}
 else
             return Request.CreateResponse(HttpStatusCode. NotFound
, null);       
            

        }
        [Route("api/Delete")]
        [AcceptVerbs("DELETE")]
        public HttpResponseMessage Delete (int studentId)
        {
           _studentList.Remove(studentList.FirstOrDefault(x=>x.studentId== studentId));
   return Request.CreateResponse(HttpStatusCode.OK,null);       

        }
[Route("api/POST")]
        [AcceptVerbs("POST")]
        public HttpResponseMessage POST(Student student)
        {
          //add syntax
           int i=1 //success
return Request.CreateResponse(HttpStatusCode.OK,null);       
 i=0 // insert fail
return Request.CreateResponse(HttpStatusCode.OK,i);       

        }

    }

How to consuming web api in asp.net mvc application using http client

                HttpClient client = new HttpClient();
                HttpResponseMessage response = new HttpResponseMessage();
                client.BaseAddress = new Uri(Uri);//api url ex: http://localhost:2454/api/
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               
var result = client.GetAsync(“Get”).Result; //method name
if(result. IsSuccessStatusCode)
{
Var data = response.Content.ReadAsAsync<Student>().Result; //model class name : model type
}


//post and delete next session-- minor mistakes