CRUD Application using ASP .Net Core Web API

Darshan Unadkat
The Startup
Published in
4 min readMay 14, 2020

--

In this article, we will see how to create a CRUD application using ASP .Net Core Web API.

Here, we will test various HTTP methods i.e GET, POST, PUT, DELETE in the POSTMAN(Download it from here). I had already created a simple application, which you can find in the below link.

Getting Started : Building Web API using ASP .Net Core

I have not used any database, but I have done this operations in-memory, which relies on main memory of the computer data storage.

Prerequisites

To achieve this task, we need to have .Net Core 3.0 SDK installed in your system. It can be verified by typing given command in your command prompt window.

If it shows error, the download it from here and install.

After that, if you have not created project then go to this article.

Clean up the code for development

In this project, they have already given some default files, but for our project some files are not needed. So, we will just delete them and configure other files as per we need in our project.

I will delete these files as it is not needed:

WeatherForecastController.cs

WeatherForecast.cs

After that, we will configure our LaunchSettings.json file. In this, we will change the launchUrl of the application as shown in the below image.

Create the Model

Here, we are going to develop a API related to Customers. So, the model for that is as given below.

Here, first we are going to create a folder named Models and then inside that we will create class Customer.cs

Create the Services

To create a service, first we need to create an Interface which will used by the service to implement the Dependency Injection.

So, create a folder named Services and inside that add new Interface named ICustomersList.cs The code for the same is as given below.

And the given below is the code for the Service.

To implement the Dependency Injection, we will have to add Service in the Startup.cs file

Now, in the Controller folder, add new Controller named HomeController.cs which will do all the CRUD operations for our proejct.

Test our API using POSTMAN

After doing all the above operations, now we will use POSTMAN to test our API.

If the build is successfully done, then it will launch the default browser in your system with port 5000(It’s shown in the LaunchSettings.json file). If it doesn’t open by default then enter localhost:5000/api/customers in the browser after run the application.

First, it will print only [] for the GET method in the body as there is no data to display.

To add a new customer we will use POST method:

To update any information, we will use PUT method with the ID of that customer in the query string:

To delete any data, we can use DELETE method with the ID of that customer in the query string same as PUT.

In delete, it will return the ID of that customer, so here it shows 1.

The code for this whole application is found at given repository.

Share your views/feedback in the comments.

Thanks.

--

--