.NET APIs Part 4 – Creating the APIs

We are, finally, ready to create our ASP.NET Core application that will host our traditional and our minimal APIs. (This series begins here.)

The code for this blog post is available here:
git clone https://github.com/JesseLiberty/Cars.git

Please note that WordPress seems to be broken and so the layout will be imperfect.

To get started, open Visual Studio 2022 and make sure you are fully up to date. Click on Create A New Project and select ASP.NET Core Web App, making sure that C# is the selected language

Give your project a name and a location

Choose .NET 7 as your Framework with no authentication and click Create.

If you’ve been following along with this tutorial already, you can steal from your earlier project. Otherwise, just type in the code.

Let’s start in appsettings.json where we will add the connection string to connect us to the database we created last time

Controllers

Key to creating your API is to create controllers. A controller is the class that holds the start of the logic that your API will use. This is much easier to demonstrate than to explain. Start by creating a folder named Controllers

Right click on your Controllers folder and create a class named CarController. For now, all our APIs will originate here. Let’s set up the top of the file by declaring a logger (supplied by Microsoft in the Microsoft.AppNetCore.MVC namespace) and an instance of our CarRepository

REST

We’ll be using REST for our APIs. There is much that you can know about REST, but for our purposes all we need to know is that we’ll be using the standard HTTP keywords to Post (Create), Get (Read), Put (Update) and Delete (Delete).

Starting Data

To have data to work with, I downloaded the Car Information Dataset from Kagel (Please read their licensing agreement before using this data elsewhere). I modified the CSV file to add an id column and an is_deleted column.

I then imported this data into a table, following the directions in this excellent tutorial. Finally, I dropped the id column and added it back as the identity column.

alter table car
add car_id_new int identity(1,1)
go

alter table car drop column id
go

exec sp_rename 'car_id_new', 'id'
go

At this point, I have a table with all the columns from the imported data, as well as an identity column.

Go to your car class (that we created in part 3) and modify it as follows (to match the incoming data):

public class Car
{
    public int id { get; set; }
    public string name { get; set; } = null!;
    public string mpg { get; set; } = null!;
    public string cylinders { get; set; } = null!;
    public string displacement { get; set; } = null!;
    public string horsepower { get; set; } = null!;
    public string weight { get; set; } = null!;
    public string acceleration { get; set; } = null!;
    public string model_year { get; set; } = null!;
    public string origin { get; set; } = null!;
    public string? is_deleted { get; set;}
}

Note that for now we are keeping things very simple. We will not declare DTOs, and so we will not deal with mapping. We’ll save that for a later blog post.

Creating Our First API

We are ready to create our first API. Its job will be to get all the cars in the database. We’ll do this by creating the API in a controller class and then invoking a method in our Car repository.

Create a Controllers folder, if you have not already, and in that folder create a CarController class. Add the following code to the top of the class:

    public class CarController : ControllerBase
    {
        private readonly ILogger<CarController> _logger;
        readonly CarRepository carRepository;

        public CarController(ILogger<CarController> logger,
            CarRepository carRepository)
        {
            _logger = logger;
            this.carRepository = carRepository;
        }

ControllerBase and ILogger are provided by Microsoft (thanks Satya!). Now, let’s create our API, which is a method decorated with the [HttpGet] attribute

 

Deceptively simple. This method returns a list of Car objects by calling GetAll on the carRepository. Let’s go look at that method:

(If you don’t already have a folder Repositories, please create one and add the class CarRepository). Here’s the top of the class:

You will need a using statement for Dapper.

We are now ready to implement the GetAll method:

    

This method will return the list of Car objects that we need, and defaults to not returning deleted records. We begin by creating a SqlBuilder – an object provided by Dapper. We then create our template, ending it with the syntax /**where**/ — this too is for Dapper and allows us to dynamically create our where clause.

You see this in the if statement; we’ll only add the where statement that restricts the results to non-deleted records if the parameter (returnDeletedRecords) is false.

Next we obtain a connection to the DataBase from our databaseConnectionFactory and finally we use that connection to call QueryAsync, identifying the type (Car) and passing in the SQL statement and the parameters to that statement, if any. In this simplified case we do not have any parameters, but we’ll see how to use them in a later blog post.

Testing with Postman

When you run your application Swagger will come up. Minimize that and open Postman, which is much more powerful and easier to work with (especially once we add authorization). Create a folder named Cars (of course, you can actually name it anything you like) and next to that folder click on the three dots that appear when you click on the name of the folder.

Choose Add Request, and rename your new request GetCars. Go to the right hand pane, and make sure the drop down is set to Get. Next to the drop down enter

https://localhost:7025/car

Be sure to substitute the port number (bold) to the one used by Swagger.

That’s all you have to do. Click Send in the upper right hand corner and your API will be called (don’t be afraid to set break points to prove this to yourself) which will call the method in the repository, which will in turn fetch your data from the database and it will all be returned to Postman…

Congratulations! You have your first working API to your back-end data stored in SQL Server!

In this coming blog posts we’ll add the remaining CRUD APIs, examine using services and DTOs and take a look at minimal APIs.


Rodrigo Juarez is a full-stack developer who has specialized in Xamarin in recent years and is now focusing on MAUI. He is also a book author. With over 25 years of experience, Rodrigo has contributed to a diverse array of projects, developing applications for web, desktop, and mobile platforms. Specialized in Microsoft technologies, he has expertise across various sectors, including management, services, insurance, pharmacy, and banking. Rodrigo Juarez can be reached at info@rodrigojuarez.com

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in API, Essentials. Bookmark the permalink.

21 Responses to .NET APIs Part 4 – Creating the APIs

  1. binance says:

    Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

  2. I am genuinely amazed with your profound understanding and excellent ability to convey information. Your expertise is evident in each paragraph. It’s evident that you invest a great deal of effort into researching your topics, and that effort pays off. Thank you for sharing this valuable knowledge. Continue the excellent job! Learn more about our OnlyFans Agency: https://elevenviral.com/onlyfans-marketing-growth-service/

  3. I’m truly impressed by the deep insights and superb ability to convey information. The knowledge you share clearly stands out in every piece you write. It’s clear that you put a lot of effort into researching your topics, and that effort does not go unnoticed. We appreciate your efforts in sharing this valuable knowledge. Continue the excellent job! Learn more about our OnlyFans Agency: https://elevenviral.com/onlyfans-marketing-growth-service/

  4. Rubi Rose OnlyFans Mega Link Download

  5. I am genuinely amazed by the profound understanding and stellar ability to convey information. The knowledge you share shines through in every sentence. It’s clear that you put a lot of effort into understanding your topics, and the results pays off. We appreciate your efforts in sharing this valuable knowledge. Keep on enlightening us! Learn more about our OnlyFans Agency: https://elevenviral.com/onlyfans-marketing-growth-service/

  6. Black Ass Jenny OnlyFans Mega Link Download

  7. Caaart OnlyFans Mega Link Download

  8. Genesis Mia Lopez OnlyFans Mega Link Download

  9. Bulma XO OnlyFans Mega Link Download

  10. Black Ass Jenny OnlyFans Mega Link Download

  11. Fitness says:

    Thanks for the guidelines you have provided here. Something else I would like to state is that personal computer memory specifications generally increase along with other developments in the know-how. For instance, if new generations of cpus are introduced to the market, there is usually a corresponding increase in the scale demands of both the pc memory and also hard drive room. This is because the software operated by simply these processor chips will inevitably increase in power to leverage the new engineering.

  12. Lamascoug says:

    I want to show you one exclusive program called (BTC PROFIT SEARCH AND MINING PHRASES), which can make you a rich man!

    This program searches for Bitcoin wallets with a balance, and tries to find a secret phrase for them to get full access to the lost wallet!

    Run the program and wait, and in order to increase your chances, install the program on all computers available to you, at work, with your friends, with your relatives, you can also ask your classmates to use the program, so your chances will increase tenfold!
    Remember the more computers you use, the higher your chances of getting the treasure!

    DOWNLOAD FOR FREE

    Telegram:
    https://t.me/btc_profit_search

Leave a Reply

Your email address will not be published. Required fields are marked *