WEB API is used to Create RESTFUL service with ease. Previously we have discussed how to create POST method, now we are going to create GET method using WEB API.

GET Method without parameter used to fetch all the records.

GET Method with parameter used to fetch the specific records which matches the parameter value.


 1.      Create new Project with WEB API as below


This will create the new project of WEBAPI in MVC framework.


2.      Right Click on the controllers and add new WEB API Controller as shown below.



Name your WEB API controller accordingly, I have named it as EmployeeController.      


3. Right click on the Model folder and add ADO.NET Entity Data Model as shown below.


Entity Data Model wizard asks you to select what the model should contain, Select EF designer from Database as shown below.   




Click on Next and Choose Data Connection by selecting new connection.


After Clicking New Connection, it will prompt the new popup to enter server details, userid, password and selecting the database.


After test connection is successful click on OK and your connection string will get generated as shown below


Select the Entity framework version and Click the Next button.


Select the Table that is necessary for accessing the data as shown below.


  Click on Finish that will generate the EDMX file with table name and columns as shown below.

  


On EmployeeController.cs we can find multiple functions remove all the except POST method and replace the code as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WEBAPIGETPROJ.Models;

namespace WEBAPIGETPROJ.Controllers
{
    public class EmployeeController : ApiController
    {
        public HttpResponseMessage Get()
        {
            try
            {
                using (ACT2_MINIQEntities entities = new ACT2_MINIQEntities())
                {
                    var employeesList = entities.EmployeeDetails.ToList();
                    return Request.CreateResponse(HttpStatusCode.OK, employeesList);
                }
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }

        public HttpResponseMessage Get(int id)
        {
            try
            {
                using (ACT2_MINIQEntities entities = new ACT2_MINIQEntities())
                {
                    var employee = entities.EmployeeDetails.FirstOrDefault(x => x.ID == id);
                    if (employee != null)
                    {
                        return Request.CreateResponse(HttpStatusCode.OK, employee);
                    }
                    else
                    {
                        return Request.CreateErrorResponse(
                            HttpStatusCode.NotFound, "Employee with id" + id + " not found"
                            );
                    }
                }
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
    }
}


Test Output using POSTMAN or Browser (because of Get request)