Creating WebAPI Project using DOTNET CLI

In this lesson, we'll create our DOT NET web Api project using the DOTNET CLI.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:09] Finally, it's time where we start working on the project. First of all, open the terminal and check if .net SDK is correctly installed.

    [00:10 - 00:19] Use the command .net - info and press enter. Now the latest version I have is 5.0.201.

    [00:20 - 00:25] It can be different when you are watching it. As you can see, I have multiple versions.

    [00:26 - 00:34] I have multiple versions of SDKs and runtimes. And as we discussed before, we can have multiple versions running simultaneously.

    [00:35 - 00:52] If you are confused what is the difference between SDK and runtime, SDK is a software development kit which includes stuff you need to build and run the application, such as the CLI and the compiler. Whereas runtime helps to run the apps which are already compiled with the SDK.

    [00:53 - 00:58] I hope it makes sense. And don't worry if version 6 is out and it becomes the latest version.

    [00:59 - 01:13] Most probably the changes won't be breaking and if there are, I will make appropriate changes to the course. Now that the software development kit is correctly installed, type .net - h to check all the available commands.

    [01:14 - 01:20] All of these commands, new helps us to create a new project or a new file. Run will build and run the application.

    [01:21 - 01:43] SLN command will create a solution file and watch command will start a file wat cher which will keep our server ready with the changes and we won't have to restart the server again and again. Now what we will do is clear the terminal and type .net new - l.

    [01:44 - 01:56] This will give us list of templates available to us to create .net related project or files. Out of these projects, we will first create a solution file using the SLN command.

    [01:57 - 02:08] Solution file will work as a container for the applications project. If you are using VS code, then solution file helps to build all those projects which are part of the solution just by running .net build command.

    [02:09 - 02:15] We will also be creating web API project. This will be the starter project which will actually run our application.

    [02:16 - 02:32] We will also be using the classlib command which helps to connect other projects to our web API. For example, if you want to create a different project that takes care of the data and interacts with the database, you can create a class library and later on add a reference to the main project.

    [02:33 - 02:40] Now that you know all the commands available, go to the directory where you want to store your project. I will create it in my test stop.

    [02:41 - 02:54] So I will go to the test stop and make the directory called learnify which is the name of our application. Now let's cd into our project.

    [02:55 - 03:08] Once you are inside the project, create a new solution file using .net new SLN command. This command will automatically take the name of the parent folder.

    [03:09 - 03:14] In our case, learnify. Now this will create a solution file in the learnify directory.

    [03:15 - 03:20] Type "ls" to see the contents of the folder. As you can see, we have the learnify.sln file here.

    [03:21 - 03:30] Now let's create a main project using the .net new web API command followed by the name of the project which will contain the contents. In our case, API.

    [03:31 - 03:41] The type .net new web API and the name of the folder is API. Press enter.

    [03:42 - 03:48] Now let's check the contents of the folder by using the ls command. Here we have our API project.

    [03:49 - 03:59] Now let's create the supporting project using the classlib command. For this type .net new classlib -n and then the name of the folder.

    [04:00 - 04:03] We will create two more. One of them is entity.

    [04:04 - 04:15] So let's create that .net new classlib -n and then the name of project. In our case entity.

    [04:16 - 04:28] Now we need another project named infrastructure. So let's do the same thing and instead of entity, let's type infrastructure.

    [04:29 - 04:38] Now let's add these projects to our solution file so that it can keep a track of all our related projects. For that we have a command .net -sln.

    [04:39 - 04:46] Let's check it out. It shows us the command we can use with .net -sln.

    [04:47 - 04:59] We will be adding the -sln add command to our three projects - API, entity and infrastructure. What this command does is it checks for the csproj file which comes with the web API and classlib projects.

    [05:00 - 05:15] csproj file contains all the dependencies with the version. So add the solution files to our project using .net -sln add. First is API project.

    [05:16 - 05:28] The second one is entity project. And finally let's add solution file to the infrastructure project.

    [05:29 - 05:43] Now we can check the files using the .net -sln list command. As you can see we have the solution files added to the API project, entity project and infrastructure project.

    [05:44 - 05:52] Now we have three projects and each project is dependent on another one. So to add the reference we have a command .net -add -reference.

    [05:53 - 05:58] Our API project has dependency on the infrastructure project. Let me explain.

    [05:59 - 06:09] Our API project will receive HTTP requests and also it will send response to the client. But it won't be interacting directly with the database.

    [06:10 - 06:19] Infrastructure project will be dealing with the database and will be sending the response to the API project. Our API project will send the response to the client.

    [06:20 - 06:30] The same way our infrastructure project is dependent on the entity project. As it will keep our interfaces which we will implement in the infrastructure project.

    [06:31 - 06:37] If you don't understand it 100% nothing to worry. It will be easier once we start working on the projects.

    [06:38 - 06:46] Now let's add references between them using these commands. Since we are in the root folder first of all let's go into the API project.

    [06:47 - 07:09] Now we want to add a reference to the infrastructure project so let's use this command.net -add -add -reference and then the path of the infrastructure project which is this. Now as you can see reference is correctly added to the project.

    [07:10 - 07:24] Now let's go to the infrastructure project and add reference to the entity project. So we'll go back one step and then we'll go inside the infrastructure project.

    [07:25 - 07:38] Now that we are the infrastructure project let's add reference to the entity project. For that.net add reference and then the path of our entity project.

    [07:39 - 07:40] Reference is added to the project. Perfect.

    [07:41 - 07:51] To check if everything is work correctly let's go back once again and type.net restore. It will restore all the dependencies.

    [07:52 - 08:01] I know this was a lot to take in but trust me it will make sense once we proceed with the code. This marks the end of this lesson and I will see you in the next one.