Building a Simple CRUD Application using ASP.NET Core 3.0 Web API

In this article, we will learn how to create a CRUD application using ASP.NET Core 3.0 Web API.

Here, I have tried to explain how to create a CRUD application using .NET Core 3.0 with various types of HTTP methods like GET, POST, PUT and DELETE (in short CRUD). I have created a sample application with name ProductsAPI and defined routes for all the four types of HTTP methods with API paths.

Note: In this article, I have not used any database. All the operations are in-memory.

Prerequisites

Before starting to build RESTful API, we need to ensure .NET Core 3.0 SDK is installed in the system or not. That can be verified by running below command in the terminal window:

dotnet --version

In my case, .NET Core SDK version is (3.0.101). If the above command does not print any valid version or prints error, then download the .NET Core 3.0 SDK and install in the system.

Create ASP.NET Core Web API project from template

Using Visual Studio, a new project for Web API can be created by following few very simple steps:

  • Open Visual Studio
  • Click on New
  • Click on App under .NET Core section in the left panel
  • Click on API under ASP.NET Core section
  • Enter Project Name
  • Enter or Browse the project location. Leave as is if the project to be created at the default location in local drive
  • Click on Create button

This will create a new project with some pre-defined controller and other associated files.

In one of my previous articles, I have explained in detail how to set up a new project for Web API using a predefined template.

Make the code Development-ready

The project, I just created using the Visual Studio template, provides some pre-defined Controller and other files that may be irrelevant for your project. Those files are irrelevant for the project I am going to develop, so I am going to clean-up the code and change the project configuration as per my need.

Remove irrelevant files

So, to make my code development-ready, I need to delete:

  • WeatherForecastController.cs
  • WeatherForecast.cs files.

Change configurations in launchSettings.json

I need to change the applicationUrl and launchUrl properties in the launchSettings.json file as shown below:

  • applicationUrl: host and port to the application
  • launchUrl: default endpoint which will be loaded when the application launches for the first time.

Create the Model

Since my sample project is related to a product, I will create a Product Model as shown below:

Create the Service

Before creating a Service, I will create an interface for my service which will be used to configure the service as Dependency Injection in the form of Singleton class at the application level.

And below is the code for my Service file:

Use the Service class as Dependency Injection in the form of Singleton class

In the Statup.cs file, add below line to configure the Service class as Dependency Injection in the form of Singleton Class. By doing this, ProductService can be accessed from anywhere throughout the application as a Singleton Class.

Define the routes for CRUD in ProductsController class

Create a class and name it as ProductController.cs.

Photo by Artem Sapegin on Unsplash

Now, its time to define our routes for the CRUD operations in our ProductController class.

Run and Test the APIs in Postman

Photo by Nonsap Visuals on Unsplash

Now, we are ready to run our code and test the APIs using Postman (a client for testing REST APIs).

We can run our code either using the run button in Studio code or by running below commands (in sequence) in terminal:

dotnet restore
dotnet build
dotnet run

If the build runs successfully(as I believe), it should launch in 5000 port (as configured in launchSettings.json file. Since we have not configured any route for default path, it might show Page can’t be found. But we should be able to launch localhost:5000/api/products. It should show a blank array in the browser window as our product list is empty.

Now, its time to open Postman and test the APIs:

First of all, we will add a few products by using POST HTTP method:

We can update the products by using PUT HTTP methods and ID in the query string:

Now, we will fetch all the products using GET HTTP method as shown below. I added two products and it should show all the products:

We can also delete any product by using DELETE method and product ID:

Sample project is available at below github location:

I hope it helped in understanding how to create CRUD APIs using .NET Core 3.0. If it helped, please share your thoughts in the comment section.

Thanks!

Fullstack Architect || TOGAF 9 || AWSCSAA || Cloud Practitioner || NodeJS || React & Angular || Docker || Coder