Introduction
In this article I will use the CRUD (Create, Read, Update, Delete) operations in the ASP. NET Web API. These operations are used in databases. These are the basic operations for databases. Here we create a simple list of Items to be managed using these CRUD operations. For using the CRUD operations in ASP. NET Web API use the following procedure.
Step 1
First we create the Web API Project.
- Start Visual Studio 2012 and select "New Project" from the Start Page.
- In the Template window, select "Installed template" -> "Visual C#" -> "Web".
- Choose "ASP. NET MVC 4 Web Application" then change its name.
- Click on the "OK" button.

- MVC 4 Project window:

- Select "Web API" and click on the "OK" button.
Step 2
Create a Model Class and name it "Item.cs".
- In the "Solution Explorer", right-click on the "Model Folder" then select "Add" -> "Class".

- In the Template window, select "Installed template" -> "Visual C#".

- Choose "Class". Change its name.
- Click on the "OK" button.
Now type this code in the "Item.cs" class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace ItemCollection.Models
- {
- public class Item
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Category { get; set; }
- public decimal Price { get; set; }
- }
- }
Step 3
Create an "Interface" and name it "ItemRepository".
- In the "Solution Explorer" right-click on the "Model folder" then select "Add" -> "New Item".

- In the Template window, select "Installed template" -> "Visual C#".

- Choose "Interface" and change its name.
- Click on the "OK" button.
Write this code in the "ItemRepository.cs":
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ItemCollection.Models
- {
- interface ItemRepository
- {
- IEnumerable<Item> GetAll();
- Item Get(int id);
- Item Add(Item item);
- void Remove(int id);
- bool Update(Item item);
- }
- }
Step 4
Create a new Model class name as "IItemRepostory.cs"
- In the "Solution Explorer" ,Right-click on the "Model Folder"->"Add"->"Class".
- In the Template window, select "Installed template"->"Visual C#".

- Choose "Class". Change its name.
- Click on "OK" button.
Write this code in the "IItemRepostory.cs" file.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace ItemCollection.Models
- {
- public class IItemRepostory:ItemRepository
- {
- private List<Item> items = new List<Item>();
- private int _nextId = 1;
- public IItemRepostory()
- {
- Add(new Item { Name = "Tomato", Category = "Vagitable", Price = 100});
- Add(new Item { Name = "Apple", Category = "Fruit", Price = 150 });
- Add(new Item { Name = "Suit", Category = "Cloth", Price = 200});
- }
- public IEnumerable<Item> GetAll()
- {
- return items;
- }
- public Item Get(int id)
- {
- return items.Find(p => p.Id == id);
- }
- public Item Add(Item item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- item.Id = _nextId++;
- items.Add(item);
- return item;
- }
- public void Remove(int id)
- {
- items.RemoveAll(p => p.Id == id);
- }
- public bool Update(Item item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- int index = items.FindIndex(p => p.Id == item.Id);
- if (index == -1)
- {
- return false;
- }
- items.RemoveAt(index);
- items.Add(item);
- return true;
- }
- }
- }
Step 5
Create a Web API Controller Class and name it "ItemsController.cs".
- In the "Solution Explorer" right-click on the "Controller Folder" then select "Add" -> "Controller".

- In the Add Controller window set the Controller name.

- Select "Template" -> "Empty API controller".
- Click on the "OK" button.
Write this code in the "ItemsController.cs" class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using ItemCollection.Models;
- namespace ItemCollection.Controllers
- {
- public class ItemsController : ApiController
- {
- static readonly ItemRepository repository = new IItemRepostory();
- public IEnumerable<Item> GetAllProducts()
- {
- return repository.GetAll();
- }
- public Item GetProduct(int id)
- {
- Item item = repository.Get(id);
- if (item == null)
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- return item;
- }
- public IEnumerable<Item> GetProductsByCategory(string category)
- {
- return repository.GetAll().Where(
- p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
- }
- public HttpResponseMessage PostProduct(Item item)
- {
- item = repository.Add(item);
- var response = Request.CreateResponse<Item>(HttpStatusCode.Created, item);
- string uri = Url.Link("DefaultApi", new { id = item.Id });
- response.Headers.Location = new Uri(uri);
- return response;
- }
- public void PutProduct(int id, Item product)
- {
- product.Id = id;
- if (!repository.Update(product))
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- }
- public void DeleteProduct(int id)
- {
- repository.Remove(id);
- }
- }
- }
Step 6






jerson gerongPosted Feb 8, 2018, 2:23 AM
Default data from ItemRepository.cs did not load in the user interface
Shakti Singh DulawatPosted Apr 17, 2016, 3:57 AM
Wonderful
bhargava kishor mPosted Apr 8, 2015, 6:56 AM
http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations
Kashif RezaPosted Jun 22, 2013, 5:39 PM
Well example is great but it is NOT working from UI. Please share the latest working code - looks like some problem with view