This is a basic tutorial about creating REST APIs using Net Core. We will start with the basics, creating the structure, adding Entity Framework, Fluent, AutoMapper, security, unit testing. Later we will talk about GraphQL and some other cool stuff.
I will add a new chapter every week. This first one is an introduction of what we want to achieve and also how to start.
Also, I will do everything in a GitHub repo so you can compare your work with mine or propose changes and enhancements.
This is not the only way to create REST API with Net Core. Although I will explain recommended practices and structures, this is just a guide. Your solution may differ from mine, and that is fine.
If you are someone that is creating for the first time a REST API with Net Core and have basic knowledge about how web services work and C#, this is for you. If you used to build apps in Net Framework and you want to do it in Net Core, this is also for you.
This can be useful also to experimented developers who wants to remember some basic steps or have a look to another way to do things.
This tutorial won't explain basic concepts or go deep into theory, but please reach me if you need further details or you want to discuss any topic.
First at all we need to install Net Core SDK, this will allow us to build and run our app. Also, will provide us useful command-line tools.
- Go to: Download .NET
- Select you OS and be sure you are downloading NET Core SDK (current version is 3.1).
- Once you downloaded the executable, run it and go through the wizard. The process is simple.
For this tutorial I will use Visual Studio Code. I prefer a lightweight IDE that doesn’t distract me from the code I want to write. If you prefer use Visual Studio it is also fine, code won’t differ because IDE choice.
One of the best things about Visual Studio Code is the vast amount of extensions we can use.
Csharp extension is a must. Basically, this will give us editing support, Intellisense, go to definitions, etc. also will provide us support for debugging. To install it, you can go to the extension tab or just run the following command:
code --install-extension ms-dotnettools.csharp
With the above extension you can start developing without a problem but I want to share another useful ones:
- .Net Core Tools: Provides a set of tools that help us build and run in a context menu in the explorer window.
- Auto-Using for C#: Auto import any library when you use it in your file.
- .Net Core Test Explorer: makes our life easier when handling unit test.
You can install them using the links, but if you are lazy just run the below commands:
code --install-extension ms-dotnettools.csharp code --install-extension formulahendry.dotnet code --install-extension Fudge.auto-using code --install-extension formulahendry.dotnet-test-explorer
Dotnet CLI is the command-line interface for developing, building, running and publishing .Net Core applications. .Net Core SDK includes this.
The first thing we should do is to create the project. For this tutorial we will build a Cook App where we can store our recipes, ingredients, etc.
To create a web api project, we need to run the new command.
As we will create our application with different layers, each one with its own concerns, we need to create multiple projects in the app's root folder. We also want to add all of them to a solution.
Let's create a new folder which I will call "cook-webapi" and add a solution file into it.
dotnet new sln -o cook-webapi
Inside the new-created folder we want to add first our api project that will be responsible to handle http requests and returns output.
dotnet new webapi --name cook-webapi.Api
Then we want to add the project to the solution.
dotnet sln add cook-webapi.Api
We can execute the fresh app just using the run command on the API project.
dotnet run --project cook-webapi.Api
We also can execute it in watch mode. This will trigger a build every time it detects a file change.
dotnet watch --project cook-webapi.Api run
Both commands will host the app in your local, by default in ports 5001 and 5000. Also, as we have done no change to the default project, we can access to the example controller endpoint: https://localhost:5001/WeatherForecast. You will see the JSON response with a weather forecast.
In the next post in this series we will create the basic structure of our solution by adding core, business and data layers. We will install Entity Framework and code everything we need to retrieve a list of recipes.
We learnt how to create a new solution file, add a webapi project and execute it. We started on the foundations of our application.
Hope this is a pleasant start. Please follow me to receive updates on the next chapters.
It's social media for devs
Level up every day