Here are the steps,
Step 1
Create a table, for example, see below with a snapshot.
Step 2
->Open Visual studio and take a new project.
->Choose ASP.NET Web Application and give name Ex.CRUDWebApiExam and click ok.
We have to create the first Web API, so we choose the Web API option and click OK.
Step 3
Now we have to add a class so for this, we right-click of web API project and add ADO.NET Entity Data Model,
Add EF Designer from the database and click the Next button.
Add click new connection option and give the server name and select database and click Test Connection and click ok.
Click Next and select the table which you want and click ok.
Click Finish.
Step 4
Now we have to add Web API controller, So right-click on controllers folder,
Select 'Web Api Controller with views, using Entity framework'.
Click Add.
Select the Model Class Name Exam. Customer(In my project).
Select Data context class, Exam. SystemTestEntity (In my project).
Create a table, for example, see below with a snapshot.

Step 2
->Open Visual studio and take a new project.
->Choose ASP.NET Web Application and give name Ex.CRUDWebApiExam and click ok.

We have to create the first Web API, so we choose the Web API option and click OK.

Step 3
Now we have to add a class so for this, we right-click of web API project and add ADO.NET Entity Data Model,

Add EF Designer from the database and click the Next button.

Add click new connection option and give the server name and select database and click Test Connection and click ok.

Click Next and select the table which you want and click ok.

Click Finish.
Step 4
Now we have to add Web API controller, So right-click on controllers folder,

Select 'Web Api Controller with views, using Entity framework'.

Click Add.
Select the Model Class Name Exam. Customer(In my project).
Select Data context class, Exam. SystemTestEntity (In my project).
Give the Controller Name Exam. Customer.
Click Add.

Note - After that it automatically generates CustomerController.cs like this.
Click Add.

Note - After that it automatically generates CustomerController.cs like this.
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- using System.Linq;
- using System.Net;
- using System.Web.Http;
- using System.Web.Http.Description;
- using CRUDWebApiExam.Models;
- namespace CRUDWebApiExam.Controllers {
- public class CustomersController: ApiController {
- private SystemTestEntities db = new SystemTestEntities();
- // GET: api/Customers
- public IQueryable < Customer > GetCustomer() {
- return db.Customer;
- }
- // GET: api/Customers/5
- [ResponseType(typeof(Customer))]
- public IHttpActionResult GetCustomer(int id) {
- Customer customer = db.Customer.Find(id);
- if (customer == null) {
- return NotFound();
- }
- return Ok(customer);
- }
- // PUT: api/Customers/5
- [ResponseType(typeof(void))]
- public IHttpActionResult PutCustomer(int id, Customer customer) {
- if (!ModelState.IsValid) {
- return BadRequest(ModelState);
- }
- if (id != customer.CustomerId) {
- return BadRequest();
- }
- db.Entry(customer).State = EntityState.Modified;
- try {
- db.SaveChanges();
- } catch (DbUpdateConcurrencyException) {
- if (!CustomerExists(id)) {
- return NotFound();
- } else {
- throw;
- }
- }
- return StatusCode(HttpStatusCode.NoContent);
- }
- // POST: api/Customers
- [ResponseType(typeof(Customer))]
- public IHttpActionResult PostCustomer(Customer customer) {
- if (!ModelState.IsValid) {
- return BadRequest(ModelState);
- }
- db.Customer.Add(customer);
- db.SaveChanges();
- return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer);
- }
- // DELETE: api/Customers/5
- [ResponseType(typeof(Customer))]
- public IHttpActionResult DeleteCustomer(int id) {
- Customer customer = db.Customer.Find(id);
- if (customer == null) {
- return NotFound();
- }
- db.Customer.Remove(customer);
- db.SaveChanges();
- return Ok(customer);
- }
- protected override void Dispose(bool disposing) {
- if (disposing) {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- private bool CustomerExists(int id) {
- return db.Customer.Count(e => e.CustomerId == id) > 0;
- }
- }
- }

Step 5
->Now we have to add an MVC project for consuming the web API services, So first we have to add a model class, so here we took Customer class. For this, we right click models folder and add class and give the name, for ex. Customer.cs.
- using System;
- using System.ComponentModel.DataAnnotations;
- namespace MVCPersatantion.Models {
- public class Customer {
- [Display(Name = "CustomerId")]
- public int CustomerId { get;
- set; }
- [Display(Name = "Name")]
- public string Name { get;
- set; }
- [Display(Name = "Address")]
- public string Address { get;
- set; }
- [Display(Name = "MobileNo")]
- public string MobileNo { get;
- set; }
- [Display(Name = "Birthdate")]
- [DataType(DataType.Date)]
- public DateTime Birthdate { get;
- set; }
- [Display(Name = "EmailId")]
- public string EmailId { get;
- set; }
- }
- }
- using MVCPersatantion.Models;
- namespace MVCPersatantion.ViewModel {
- public class CustomerViewModel {
- public Customer customer { get;
- set; }
- }
- }
We have to add a class for consuming the web service for this thing so we add a class and give the name exam. CustomerClient.cs for this just right click on Models folder and add below code.
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Net.Http.Headers;
- namespace MVCPersatantion.Models {
- public class CustomerClient {
- private string Base_URL = "http://localhost:30110/api/";
- public IEnumerable < Customer > findAll() {
- try {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(Base_URL);
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.GetAsync("customers").Result;
- if (response.IsSuccessStatusCode)
- return response.Content.ReadAsAsync < IEnumerable < Customer >> ().Result;
- return null;
- } catch {
- return null;
- }
- }
- public Customer find(int id) {
- try {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(Base_URL);
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.GetAsync("customers/" + id).Result;
- if (response.IsSuccessStatusCode)
- return response.Content.ReadAsAsync < Customer > ().Result;
- return null;
- } catch {
- return null;
- }
- }
- public bool Create(Customer customer) {
- try {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(Base_URL);
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.PostAsJsonAsync("customers", customer).Result;
- return response.IsSuccessStatusCode;
- } catch {
- return false;
- }
- }
- public bool Edit(Customer customer) {
- try {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(Base_URL);
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.PutAsJsonAsync("customers/" + customer.CustomerId, customer).Result;
- return response.IsSuccessStatusCode;
- } catch {
- return false;
- }
- }
- public bool Delete(int id) {
- try {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(Base_URL);
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.DeleteAsync("customers/" + id).Result;
- return response.IsSuccessStatusCode;
- } catch {
- return false;
- }
- }
- }
- }
->First we see in the first line I gave base URL. see below
private string Base_URL = "http://localhost:30110/api/";
So I want to say that this base_url value is the web API service URL,

->After we write methods for Insert, Update, Delete, Select one by one
Step 7
We have to add a controller, so for this right-click on the Controllers folder and add a controller and give the name for ex.Customers and write the code and call the service client method.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVCPersatantion.Models;
- using MVCPersatantion.ViewModel;
- namespace MVCPersatantion.Controllers {
- public class CustomerController: Controller {
- // GET: Customer
- public ActionResult Index() {
- CustomerClient CC = new CustomerClient();
- ViewBag.listCustomers = CC.findAll();
- return View();
- }
- [HttpGet]
- public ActionResult Create() {
- return View("Create");
- }
- [HttpPost]
- public ActionResult Create(CustomerViewModel cvm) {
- CustomerClient CC = new CustomerClient();
- CC.Create(cvm.customer);
- return RedirectToAction("Index");
- }
- public ActionResult Delete(int id) {
- CustomerClient CC = new CustomerClient();
- CC.Delete(id);
- return RedirectToAction("Index");
- }
- [HttpGet]
- public ActionResult Edit(int id) {
- CustomerClient CC = new CustomerClient();
- CustomerViewModel CVM = new CustomerViewModel();
- CVM.customer = CC.find(id);
- return View("Edit", CVM);
- }
- [HttpPost]
- public ActionResult Edit(CustomerViewModel CVM) {
- CustomerClient CC = new CustomerClient();
- CC.Edit(CVM.customer);
- return RedirectToAction("Index");
- }
- }
- }
Step 8:
->Now we have to create a view page so first create the Index page.
->For this right-click on index method in Customer controller and add view and write below code
Index.cshtml
->Now we have to create a Create page.
->For this right-click on create method in Customer controller and add view and write below code.
Create.cshtml
->Now we have to create edit view page.
->For this right-click on Edit method in Customer controller and add view and write below code
Edit.cshtml
->Now we have to create a view page so first create the Index page.
->For this right-click on index method in Customer controller and add view and write below code
Index.cshtml
- @{ViewBag.Title = "Index";}
- <h2>Index</h2>
- <div align="center">
- <a href="@Url.Action(" Create"," Customer ")"> Add New Customer </a> <br /> <br />
- <table cellpadding="2" class="table" cellspacing="2" border="1">
- <tr class="btn-primary">
- <th> CustomerId </th>
- <th> Name </th>
- <th> Address </th>
- <th> MobileNo </th>
- <th> Birthdate </th>
- <th> EmailId </th>
- <th> Action </th>
- </tr>
- @foreach(var Cust in ViewBag.listCustomers) { <tr class="btn-success">
- <td>@Cust.CustomerId </td>
- <td>@Cust.Name </td>
- <td>@Cust.Address </td>
- <td>@Cust.MobileNo </td>
- <td>@Cust.Birthdate.ToString("dd/MM/yyyy") </td>
- <td>@Cust.EmailId </td>
- <td>
- <a onclick="return confirm('Do you want to Delete?')" href="@Url.Action(" Delete "," Customer ",new {id= Cust.CustomerId})">Delete</a> || <a href="@Url.Action(" Edit "," Customer ",new { id = Cust.CustomerId
- })
- ">Edit</a> </td>
- </tr>
- }
- </table>
- </div>
->For this right-click on create method in Customer controller and add view and write below code.
Create.cshtml
- @{ViewBag.Title = "Create";
- }
- @model MVCPersatantion.ViewModel.CustomerViewModel
- <h2> Create </h2>
- <div align="center">
- @using(Html.BeginForm("create", "Customer", FormMethod.Post))
- {
- <table class="table">
- <tr>
- <td>@Html.LabelFor(model => model.customer.Name) </td>
- <td>@Html.TextBoxFor(model => model.customer.Name) </td>
- </tr>
- <tr>
- <td>@Html.LabelFor(model => model.customer.Address) </td>
- <td>@Html.TextBoxFor(model => model.customer.Address) </td>
- </tr>
- <tr>
- <td>@Html.LabelFor(model => model.customer.MobileNo) </td>
- <td>@Html.TextBoxFor(model => model.customer.MobileNo) </td>
- </tr>
- <tr>
- <td>@Html.LabelFor(model => model.customer.Birthdate) </td>
- <td>@Html.TextBoxFor(model => model.customer.Birthdate) </td>
- </tr>
- <tr>
- <td>@Html.LabelFor(model => model.customer.EmailId) </td>
- <td>@Html.TextBoxFor(model => model.customer.EmailId) </td>
- </tr>
- <tr>
- <td></td>
- <td><input type="submit" value="Save" /></td>
- </tr>
- </table>
- } </div>
->For this right-click on Edit method in Customer controller and add view and write below code
Edit.cshtml
- @ { ViewBag.Title = "Edit";}
- @model MVCPersatantion.ViewModel.CustomerViewModel
- <h2> Edit </h2>
- <div align="center" width="500px"> @using(Html.BeginForm("Edit", "Customer", FormMethod.Post))
- {
- <table class="table" width="400px" cellpadding="20">
- <tr class="btn-default">
- <td> @Html.LabelFor(model => model.customer.Name) </td>
- <td> @Html.TextBoxFor(model => model.customer.Name) </td>
- </tr>
- <tr>
- <td> @Html.LabelFor(model => model.customer.Address) </td>
- <td> @Html.TextBoxFor(model => model.customer.Address) </td>
- </tr>
- <tr>
- <td> @Html.LabelFor(model => model.customer.MobileNo) </td>
- <td> @Html.TextBoxFor(model => model.customer.MobileNo) </td>
- </tr>
- <tr>
- <td> @Html.LabelFor(model => model.customer.Birthdate) </td>
- <td> @Html.TextBoxFor(model => model.customer.Birthdate) </td>
- </tr>
- <tr>
- <td> @Html.LabelFor(model => model.customer.EmailId) </td>
- <td> @Html.TextBoxFor(model => model.customer.EmailId) </td>
- </tr>
- <tr>
- <td> </td>
- <td> <input type="submit" value="Save" /> & nbsp; <a class="btn-primary" href="@Url.Action(" Index "," Customer ")"> Back </a> @Html.HiddenFor(model => model.customer.CustomerId) </td>
- </tr>
- </table>
- } </div>
->Finally we have completed it so now we execute the program but one thing here is we have to run both web API and MVC projects so first, we have to set this that runs both programs.
->So for a set this, we right-click on the solution and go to properties like this.

->So next, we go to Startup Project option and select the multiple startup projects and go to Action and set 'Start' for both project, that's all
->So finally execute the program and see the result and see the functionality of crud operation.
First, see for the select result,

See for the Insert,

See for Update,

See for delete,

Click ok.
I hope you enjoyed this. Thank you for looking at the example.

Raichand RayPosted Sep 13, 2021, 1:21 PM
What is ADO.NET Entity Data Model Model Name? It creates .edmx file. what is its name? After Adding this model project is to be built.
Wen ChangPosted Jan 7, 2021, 3:45 AM
Thank you for share .It's heplful
Deepak YadavPosted Dec 30, 2020, 1:20 PM
Second project how to create this step missing.
shubham kalePosted Aug 11, 2020, 12:26 AM
I am getting CustId and Emil is null why?value present in Db
DanPosted May 23, 2020, 2:37 AM
When I am running the application, I get that the ViewBag.listCustomers for Index is null. I got null in findCustomers method. What am I missing here ?
vincent sorianoPosted Jul 2, 2019, 10:41 PM
I'm a beginner and it worked for me, but upon creating another crud on the project the design changed. Where should I check, I followed the same procedure from the customer, I just added the employee.
Sarathlal SaseendranPosted Jun 21, 2019, 9:54 AM
Very good article
Donna HarrisPosted Jun 13, 2019, 12:41 PM
Great article, But instead using JSON data , as defined in the CustomerClient class, Can you show How we could do this same application using XML data ? I've tried changing the line in the FindAll method --> client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/XML") But it will not work with XML Can you show me using this example, could use XML data ? Thank you
Rohan RaoPosted May 28, 2019, 3:26 AM
When I write the following code in Controller: [HttpPost] public ActionResult Create(CustomerViewModel cvm) it gives me error and says the method is not present. How to solve this problem, please help.
Pergin SheniPosted Mar 12, 2019, 5:14 AM
Step 4 is not correct, have to choose web api 2 controller instead of an ordinary controller.
Digit PrincePosted Feb 21, 2019, 2:24 AM
Thank you.
Dharmendra KumarPosted Oct 9, 2018, 5:47 AM
Can we use store procedure for web Api?
আনিছ আনিছPosted Sep 15, 2018, 10:20 PM
I have got the help from your structure.Great Article it help me...
Saurabh KumarPosted Jul 4, 2018, 4:34 AM
To Add Controller use Template > Select WEB API 2 Controller with actions, using Entity Framework
Saurabh KumarPosted Jul 4, 2018, 4:32 AM
After step 3 , do build the project
Biplab DasPosted Mar 19, 2018, 9:47 AM
I have got the help from your structure.
Neeta sanasPosted Mar 1, 2018, 5:24 AM
I got following error plz help
Jeet AgrawalPosted Jan 6, 2018, 1:11 PM
Thanks, Articles are very help full to understand the web api crud operation.
Sidhartha PatnaikPosted Jan 2, 2018, 10:27 PM
Where is the 'Web Api Controller with views, using Entity framework' when adding a controller ?? I did not find..
PradeepPosted Jun 26, 2017, 6:23 AM
Nice article.............
Nick ShivharePosted Jun 25, 2017, 5:02 AM
Nice article............
Manav PandyaPosted Jun 1, 2017, 12:53 AM
Nice share ...........................
James WrightPosted Jun 27, 2016, 11:24 AM
Why call async methods synchronously? You're just blocking the current thread of execution that IIS has created for the request, whereas you could also be taking advantage of the CLR's thread pool. public async IEnumerable<Customer> FindAllAsync() { [...] HttpResponseMessage response = await client.GetAsync("customers"); } This will allow you to handle a higher number of requests at any given time.
Pradeep SahooPosted Jun 17, 2016, 10:21 PM
well written , Thanks
sandy hrkPosted Jun 16, 2016, 8:16 AM
nice article...
kalu singh raoPosted Jun 15, 2016, 2:57 PM
nice...
Sabyasachi MishraPosted Jun 10, 2016, 12:47 AM
Good one
Vignesh ManiPosted Jun 3, 2016, 12:59 PM
NIce one
Kishor Bikram OliPosted Jun 2, 2016, 11:51 PM
Lovely. Going to host an web API for my mobile apps.
Mithilesh KumarPosted May 30, 2016, 3:46 AM
After adding the model class, build the solution
Ankit KUmarPosted May 28, 2016, 2:00 AM
Nice Nice
Debasis SahaPosted May 27, 2016, 2:05 PM
Nice sharing..
ismail aakhilPosted May 27, 2016, 12:40 PM
Thanks
Mithilesh KumarPosted May 27, 2016, 12:06 PM
Thanks...
Sonu ChaudharyPosted May 27, 2016, 10:10 AM
good one info..
Ketak BhalsingPosted May 27, 2016, 7:53 AM
Nice Info....
Ankur MistryPosted May 27, 2016, 6:19 AM
Very Nice
Mithilesh KumarPosted May 27, 2016, 6:17 AM
Thanks @Shridhar Sharma
Shridhar SharmaPosted May 27, 2016, 5:51 AM
nice share Mithilesh
Mithilesh KumarPosted May 26, 2016, 2:39 PM
thanks @Sonu Chaudhary
Sonu ChaudharyPosted May 26, 2016, 10:19 AM
good one
Dushmantha BaranigePosted May 26, 2016, 8:54 AM
When i am creating APIController by using Step 04, it's generating normal Controller not APIController ,Why is That ?
Vignesh ManiPosted May 26, 2016, 8:35 AM
Nice
Mithilesh KumarPosted May 26, 2016, 8:12 AM
Thanks@ farooq smd
farooq smdPosted May 26, 2016, 7:47 AM
nice one
Mithilesh KumarPosted May 26, 2016, 5:36 AM
Thanks @Raveendra Reddy Chitapana
Raveendra Reddy ChitapanaPosted May 26, 2016, 5:26 AM
Good One
Ketak BhalsingPosted May 26, 2016, 2:11 AM
Nice Info...
Humayun Kabir MamunPosted May 26, 2016, 1:57 AM
Nice...
Mithilesh KumarPosted May 26, 2016, 1:37 AM
Thanks @Debasis Saha
Debasis SahaPosted May 26, 2016, 1:19 AM
Good One..
Mithilesh KumarPosted May 26, 2016, 12:54 AM
Thanks@ Akshay Phadke
Akshay PhadkePosted May 25, 2016, 11:59 PM
Nice
Mithilesh KumarPosted May 25, 2016, 3:04 PM
Thank you
Kuppurasu NagarajPosted May 25, 2016, 1:08 PM
Nice Sharing..