How to create CRUD operations using ASP.NET Web API 2?

The goal of this post is to explain how to create CRUD operations using ASP.NET Web API 2. I’ll be using attribute routing for CRUD operations that will be consumed by a client.

CRUD Operations

CRUD stands for create, read, update and delete a particular resource.

To keep the scope of this post simple, I’ll not use a database to persist data. I’ll be writing another post as an update. So, please subscribe to my blog to get an update.

I’ll be handling persistence in my application’s memory. So, any object I’ll create, update or delete, will be available just for that session.

Project Template & IDE

The template that I’ve used for this project is a ASP.NET Web API template in Visual Studio 2015.

Scenario

There is a requirement to create a prototype of a e-commerce application. The name of this application is Prodo. We need to manage categories and products for this application. So, a category can have many products.

I’m using this relation for this post so that I can explain how attribute routing can help us in this case in the post below.

The definition of Category and Product type is very simple. In real world, it may have many other members. Definition of these two types is shown below:

Next, as I said above, I’m not using a database for persisting. So, I’ll create a layer over the in-memory objects using a Repository pattern. The API controllers that will consume this repository doesn’t need to care what is the type of data layer. In my future posts, I’ll move in-memory persistence to database.

The definition of CategoryRepository & ProductRepository classes is show below. Note that I’ve also created an interface for each repository that the concrete repository class will need to implement. This is useful for unit testing as well as Dependency Injection.

You will see that CategoryRepository has only two methods and ProductRepository has four methods. The latter one is more interesting as it handles CRUD operations with in-memory persistence.

As the CategoryRepository is simple, the CategoryController is also very simple with just two methods:

Let us now look at the ProductsController class to understand what Web API 2 has to offer:

Let us now understand the code above.

ProductsController uses ProductRepository to create, read, update and delete a Product object.

Resources often contain child resources: Categories have products, Customers have orders, movies have actors, books have authors, and so forth. It’s natural to create URIs that reflect these relations: /categories/1/products.

Please read my this post, if you would like to learn more about Attribute Routing based on code above. Understanding attribute routing with parameters, constraints is really important.

Next we use different implementations of IHttpActionResult type like OK (for HTTP 200 response) and NotFound (for HTTP 404 response) as response. A relevant response is used based on a what repository returns.

If you run this application and try to consume these REST services using an HTTP client like Fiddler or Postman, you will be able to create, read, update and delete a Product and read Categories.

Make sure that you use Content-Type: application/json in headers when using any HTTP client to make HTTP POST, PUT and DELETE requests to the server.

Hope this post explains how to create CRUD operations using ASP.NET Web API 2. If you would like to learn more about this framework, please read my other ASP.NET Web API V2 posts.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger