DEV Community

Cover image for Create a web API with ASP.NET Core 6
EneasLari
EneasLari

Posted on

Create a web API with ASP.NET Core 6

Months ago started working on a freelance project for a freelance customer, I decided to make a custom solution for the project using .Net core 6, React and Postgre SQL. It is a stack that I never had used before so it was a bit of challenging to me.

So today I will start a series of tutorials on how to start working with this stack.

This tutorial is about creating a Rest API using .Net Core 6.

We need the following tools installed on our computer:

  • Visual Studio 2022
  • .NET 6.0 SDK
  • MS SQL Server
  • SSMS

Let’s get started.

Creating a Web API Project

Open Visual Studio 2022 and select Create a new project and then select ASP.NET Core Web API:

Starting new project on visual studio 2022

then give a name to your project and click Next.

configure your new project

In the next screen, select .NET 6.0 as the framework and click Create:

select the dot net core version

At this point we have a starter project as follows:

image of solution

We are ready to do magic with some code.

First let's run the default project:

Press the start button on visual studio, by default a new browser window will open with a swagger page with the one endpoint existing.

Otherwise after run you can see the API running on :
https://localhost:7246/swagger/index.html

Image of project running

When we run the application, the default URL comes from the launchSettings.json:

image of the project setting where it shows in which address and ports runs locally

And the result values come from the GET method of the WeatherForecastController:

iamge of the get endpoint on code

Adding a Model

In Solution Explorer, right-click the project.
Select Add -> New Folder and name the folder Models.

Then right-click the Models folder and select Add->Class.
Name the class Product.cs and click Add.

Next, add the following properties to the class:

image of the product model

The Id field is required by the database for the primary key.

Entity Framework Core

We will use our model with Entity Framework Core (EF Core) to work with a database.

EF Core is an object-relational mapping (ORM) framework that simplifies the data access code. Model classes don’t have any dependency on EF Core. They just define the properties of the data that will be stored in the database.

In this post, we will write the model classes first and EF Core will create the database. This is called Code First Approach.

Let’s add the EF Core NuGet packages to the project. Right-click on the project and select Manage NuGet Packages… and then install the following packages:

Microsoft.EntityFrameworkCore:
Image microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.SqlServer:
Image microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools:
microsoft.EntityFrameworkCore.Tools

Adding a Database Context
The database context is the main class that coordinates Entity Framework functionality for a data model. This class is created by deriving from the Microsoft.EntityFrameworkCore.DbContext class.

Now, right-click the Models folder and select Add ->Class. Name the class ProductContext and click Add. Then add the following code to the class:

Image of ProductContext

The preceding code creates a DbSet property for the entity set.

In Entity Framework terminology, an entity set typically corresponds to a database table and an entity corresponds to a row in the table.

The name of the connection string is passed into the context by calling a method on a DbContextOptions object. For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file.

We need to add our connection string to the appsettings.json. I will use the local SQL server instance in my machine and we can define the connection string as follows:

image of connection string

Now, we will register our database context to the built-in IOC container. Add the following code to Program.cs:

Image of program after adding connection to dependency injection

Creating Database with Migrations

Now, we will create the database using the EF Core Migrations feature.

First, we will add an initial Migration.

Open Tools -> NuGet Package Manager > Package Manager Console(PMC) and run the following command in the PMC:

Add-Migration Initial

The Add-Migration command generates code to create the initial database schema which is based on the model specified in the ProductContext class. The Initial argument is the migration name and any name can be used.

After running the command, a migration file is created under the Migrations folder:

image of migration command executed

As the next step, run the following command in the PMC:

Update-Database

The Update-Database command runs the Up method in the Migrations/{time-stamp}_Initial.cs file, which creates the database.

Now, we will check the database created. Open View -> SQL Server Object Explorer.

You will see the newly created database as below:

Image description

As you see, the Product table and the Migrations History table are created automatically. Then a record is inserted into the migration history table to show the executed migrations on the database.

Creating API Controller and Methods

In this section, we will create the Products API Controller and add the methods to it, and also will test those methods.

Let’s add the controller first. Right-click on the Controller folder and select Add -> Controller.. and then select API Controller - Empty as below:

Image description

Click Add and give a name to your controller on the next screen.

Image description

ProductsController is created as below:

Image description

As you see, the class is decorated with the [ApiController] attribute. This attribute indicates that the controller responds to web API requests.

ProductsController class inherits from ControllerBase.

Next, we will inject the database context mentioned in the previous section through the constructor of the controller. Add the following code:

Image description

Now, we will add CRUD (create, read, update, and delete) action methods to the controller.

Let’s start with the GET methods.

GET Method

Add the following code to the ProductsController:

Image description

GetProducts method returns all the products and GetProductById(int id) method returns the product having the Id given as input. They are decorated with the [HttpGet] attribute which denotes that a method responds to an HTTP GET request.

The return type of the GetProduct methods is ActionResult type. ASP.NET Core automatically serializes the object to JSON and writes the JSON into the body of the response message. The response code for this return type is 200, assuming there are no unhandled exceptions. Unhandled exceptions are translated into 5xx errors.

Routing and URL Paths

The URL path for each method is constructed as follows:

Start with the template string in the controller’s Route attribute (Route("api/[controller]")). Then replace [controller] with the name of the controller, which by convention is the controller class name minus the Controller suffix. For this sample, the controller class name is ProductsController, so the controller name is products.

We can test the app by calling the two endpoints from a browser as follows:

Or we can test them via swagger at https://localhost:7246/swagger/index.html

But first you should add some test data through SQL Server Management Studio

We do not need to add data for the Id column as SQL Server automatically handles this for us.

Now, we can test the GET endpoints. Start (Ctlr+F5) the application:

Image of swagger call

POST Method

Add the following code to the ProductsController:

Image post product

PostProduct method creates a ptoduct record in the database. The preceding code is an HTTP POST method, as indicated by the [HttpPost] attribute. The method gets the value of the product record from the body of the HTTP request.

The CreatedAtAction method:

  • Returns an HTTP 201 status code, if successful. HTTP 201 is the standard response for an HTTP POST method that creates a new resource on the server.
  • Adds a Location header to the response. The Location header specifies the URI of the newly created product record.
  • References the GetProductById action to create the Location header's URI.

Testing the PostProduct Method

Start the application and then select the POST method:

Image description

Response:

Image description

Also, we can check this record from the Products table in our local database:

Image description

PUT Method

Add the following code to the ProductsController:

Image description

Test the PutProduct Method:

Image description

Response:

Image description

DELETE Method
Add the following code to the ProductsController:

Image description

Testing the DeleteProduct Method

Image description

Response:

Image description

Now we have implemented the main CRUD actions for our Product entity.

I hope you found this post helpful.

Top comments (1)

Collapse
 
abelardusbm profile image
Abelardus • Edited

It sounds very interesting! A question I have is what was the main goal to build this framework for the client? What are we looking to solve? Thanks!