How To API in .NET
Published: 2022-05-08 17:20:52

##PersonController

```c#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonAPI.Models;

using System.Web.Http;

namespace PersonAPI.Controllers
{
    public class PersonController : ApiController
    {
        DB DBlayer = new DB();

        string message = string.Empty;
        // GET: Person
        

        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        [System.Web.Http.HttpGet]

        public DataSet GetRecord(int id)
        {
            DataSet ds = DBlayer.GetRecordbyID(id, out message);
            return ds;
        }

        [System.Web.Http.HttpPost]
        public string PostCreatePerson([FromBody] PersonModel person)
        {
            string message = "";
            message = DBlayer.InsertPerson(person);
            return message;
        }

        [System.Web.Http.HttpPut]

        public string PutUpdatePerson(int id, [FromBody] PersonModel person) 
        {
            string message = "";
            message = DBlayer.UpdatePerson(person);
            return message;
        }

        [System.Web.Http.HttpDelete]

        public string DeletePerson(int id)
        {
            string message = "";
            message = DBlayer.DeletePerson(id);
            return message;
        }
    }
}
```

## DB.cs

```c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using PersonAPI.Models;

namespace PersonAPI
{
    public class DB
    {
        public SqlConnection ConnecttoDB()
        {
            string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='D:\NUV\Second Year\Sem 4\ASP.NET\PersonAPI\PersonAPI\App_Data\Person.mdf';Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            return con;

        }

        public DataSet GetRecordbyID(int id, out string message)
        {
            message = "";
            SqlConnection con = ConnecttoDB();
            SqlCommand com = new SqlCommand("getPersonbyID", con);
            com.CommandType = System.Data.CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@id", id);

            SqlDataAdapter adp = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            adp.Fill(ds);

            return ds;
            con.Close();
            message = "Success";
        }

        public string InsertPerson(PersonModel person)
        {
            string message = "";
            SqlConnection con = ConnecttoDB();
            SqlCommand com = new SqlCommand("gpInsertPerson", con);
            com.CommandType = CommandType.StoredProcedure;

            com.Parameters.AddWithValue("@id", person.id);
            com.Parameters.AddWithValue("@Name", person.Name);
            com.Parameters.AddWithValue("@City", person.City);
            com.Parameters.AddWithValue("@Phoneno", person.phoneno);

            com.ExecuteNonQuery();
            message = "Success";
            return message;

        }

        public string UpdatePerson(PersonModel person)
        {
            string message = "";
            SqlConnection con = ConnecttoDB();
            SqlCommand com = new SqlCommand("gpUpdatePerson", con);
            com.CommandType = CommandType.StoredProcedure;

            com.Parameters.AddWithValue("@id", person.id);
            com.Parameters.AddWithValue("@Name", person.Name);
            com.Parameters.AddWithValue("@City", person.City);
            com.Parameters.AddWithValue("@Phoneno", person.phoneno);

            com.ExecuteNonQuery();
            message = "Success";
            return message;

        }

        public string DeletePerson(int id)
        {
            string message = "";
            SqlConnection con = ConnecttoDB();
            SqlCommand com = new SqlCommand("spDeletePerson", con);
            com.CommandType = CommandType.StoredProcedure;

            com.Parameters.AddWithValue("@id", id);
            

            com.ExecuteNonQuery();
            message = "Success";
            return message;

        }

    }

}
```

## Model

```c#
public int id { get; set; }

public string Name { get; set; }

public string City { get; set; }

public string phoneno { get; set; }
```

##Store Procedure
Delete Person
```c#
CREATE PROCEDURE [dbo].[DeletePerson]
	@id int
AS
BEGIN
	delete from Person where Id = @id;
END;
```

Insert
```c#
CREATE PROCEDURE [dbo].[getInsertPerson]
	@id int,
	@name varchar(50),
	@city varchar(50),
	@phone nvarchar(50)
AS
BEGIN
	insert into Person (ID, Name, City, Phone) values (@id,@name, @city, @phone);
END;
```

getPerson
```c#
CREATE PROCEDURE [dbo].[getPerson]
	
AS
BEGIN
	SELECT * from Person
END;
```

getPersonID
```c#
CREATE PROCEDURE [dbo].[getPerson]
	
AS
BEGIN
	SELECT * from Person
END;
```

update person
```c#
CREATE PROCEDURE [dbo].[getUpdatePersonn]
	@id int,
	@name varchar(50),
	@city varchar(50),
	@phone nvarchar(50)
AS
BEGIN
	Update Person set Name = @name, City = @city, Phone = @phone where Id = @id
END
```



## Folder Structure

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1s68fyxhm9zh2k37and.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqv9czw7d7omgywm7669.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugryywv58ybhvrgntzg3.png)
[Read More](https://firebasestorage.googleapis.com/v0/b/statictube-5ddae.appspot.com/o/files%2FPersonAPI.zip?alt=media&token=4d619418-0907-47e3-be5b-ec186bb809d4)