Build web api in c#

In this section we will discuss about how to create a web API in ASP.NET MVC.

Let's create a Web API with ASP.NET and latest version of the Entity Framework. And follow these below steps:

Step-1:
  1. Open Visual Studio, and select menu File > New > Project > Visual C# - Web > ASP.NET  Web Application 
  2. Set the project name WorksWithWebAPI and click OK.
  3. Select "WebApi" template,set "No Authentication", unchecked "Host in the cloud" options and click OK.
  4. After that, WebApi Solution will created.

Step-2:


Now Create Database "InductionNew" and Add table "dbo.Tbl_Student_Api" in Database


CREATE TABLE [dbo].[Tbl_Student_Api] (

    [Student_Id]       INT          IDENTITY (1, 1) NOT NULL,

    [Student_Name]     VARCHAR (50) NULL,

    [Student_Age]      INT          NULL,

    [Student_Gender]   VARCHAR (50) NULL,

    [Student_Religion] VARCHAR (50) NULL,
    PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);

Step-3:
Add Entity "InductionNewEntities1" in model directory.

        Step-4:

        Now add Controller "DefaultController.cs" in controller folder in this solution. Write these below code in "DefaultController.cs".

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Net;
        using System.Net.Http;
        using System.Web.Http;
        using Newtonsoft.Json.Linq;
        using System.Text;
        using System.Data.Entity;
        using System.Data.Entity.Core;
        using WebAAPP.Models;

        namespace WebAPI.Controllers
        {
            public class DefaultController : ApiController
            {
                InductionNewEntities1 ob = new InductionNewEntities1();
                // Get: api/Default
                public HttpResponseMessage Get()
                {
                    try
                    {
                        var data = (from a in ob.Tbl_Student_Api select a).ToList();
                        return new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new StringContent(JArray.FromObject(data).ToString(), Encoding.UTF8, "application/json")
                        };
                    }
                    catch (Exception)
                    {
                        return new HttpResponseMessage(HttpStatusCode.BadRequest);
                    }
                }
                // Get: api/Default/5
                public HttpResponseMessage Get(int?id)
                {
                    var val = (from a in ob.Tbl_Student_Api.ToList()
                               where a.Student_Id == id
                               select a).ToList();
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent(JArray.FromObject(val).ToString(), Encoding.UTF8, "application/json")
                    };
                }
                // POST: api/Default
                public HttpResponseMessage Post([FromBody]Tbl_Student_Api on)
                {
                    Tbl_Student_Api apl = new Tbl_Student_Api();
                    apl.Student_Name = on.Student_Name;
                    apl.Student_Religion = on.Student_Religion;
                    apl.Student_Age = on.Student_Age;
                    apl.Student_Gender = on.Student_Gender;
                    ob.Tbl_Student_Api.Add(apl);
                    ob.SaveChanges();
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
                // PUT: api/Default/5
                public HttpResponseMessage Put([FromBody]Tbl_Student_Api on,int?id)
                {
                    try
                    {
                        Tbl_Student_Api apl = ob.Tbl_Student_Api.Where(a => a.Student_Id == id).FirstOrDefault();
                        apl.Student_Name = on.Student_Name;
                        apl.Student_Religion = on.Student_Religion;
                        apl.Student_Age = on.Student_Age;
                        apl.Student_Gender = on.Student_Gender;
                        ob.SaveChanges();
                        return new HttpResponseMessage(HttpStatusCode.OK);
                    }
                    catch (Exception)
                    {
                        return new HttpResponseMessage(HttpStatusCode.BadRequest);
                    }
                }
                // DELETE: api/Default/5
                public HttpResponseMessage DeleteStudentDetails(int Student_Id)
                {
                    Tbl_Student_Api apl = ob.Tbl_Student_Api.Where(a => a.Student_Id == Student_Id).FirstOrDefault();
                    ob.Tbl_Student_Api.Remove(apl);
                    ob.SaveChanges();
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
            }

        }

        Step-5:

        Build this solution and run it in browser or Fiddler.

        Output:


        Post a Comment

        1 Comments

        1. Hi, I keep getting error "namespace could not be found" when I try to add the entity in models, could please show to add it? maybe I adding it wrong

          ReplyDelete