Reviewing the Project Files
In this lesson, we'll go through the entire project that comes with Web API project and Class library Project
Reviewing project files#
Our projects are now ready for the action, and what you need to do is open the IDE or code editor of your choice; I am using VS Code. Let's open the learnify project. I will type code followed by a space and command . to open the project directly from the terminal. If you want to do the same, open VS code, open the command palette using cmd shift P on Mac and ctrl shift P on Windows, and type
shell command to find the Shell Command: Install 'code' command in PATH command. Now select this option, you will get a little notification saying Shell command 'code' successfully installed in PATH.. Now you're ready to open any project from your terminal.
Once you open the project in VScode, you will get a little popup asking Required assets to build and debug are missing from 'learnify'. Add them?. This notification is from the C# extension that we installed. And we want to add this, so press yes. If you pressed not now already, don't worry, open the command palette again and type
generate. It will give you an option, Generate Assets for Build and Debug. Select this option and you will find a
.vscode directory in your project. The directory contains
tasks.json files. We will talk about them once we need them.
As you can see, we have 3 directories;
Infrastructure and a solution file,
learnify.sln. Let's look at
learnify.sln first. It's the structure file for organizing projects. It contains text-based information about the project environment and project state. For example, if you search for API; there you have it, it has all our projects and it manages the state of our projects.
Infrastructure directory &
Entity directory, there are two files and two folders each. One is
csproj file, which takes care of all the dependencies. It tells us the SDK version we are using, and it also has information about the references that we have added in the last lesson. They also contain
Class1.cs file, which is, unfortunately, of no use to us. So to take care of them, we'd delete them. There must be two more folders which are of no use to us. I have taken care of them by excluding them in my editor. If you are seeing them and want to get rid of them, open Settings and type
exclude. Click the add pattern, and add
**/obj. Press OK and exit. I have excluded them because they appear everytime you create a new Dot Net project, and you have to manually delete them every single time. This process takes care of the manual deletion work.
Now let's talk about our API project. This must be looking different from the other two directories because remember, this is our Web API project and also our starter project, every request will first hit this project before going anywhere else.
Properties folder, there is a
launchsettings.json file. The settings that are present within this file are going to be used when we run the .NET application. Since we have just created the project, the environment is
Development, and depending on which IDE we are using, different profiles will be invoked. If you're using Visual Studio, it is going to use the IISExpress, which will start the server on the random url. For us, if you're using Visual Studio Code, it will use the API object, which will run our server on http server 5000 and https server 5001. By default, it launches the browser when the server starts. I find that annoying, so I'll turn it to false; you can choose as per your convenience. By the way, it uses Swagger on the browser to list our API endpoints. If you don't know what Swagger is, Swagger allows you to describe the structure of your APIs so that machines can read them. Let's also get rid of the https endpoint, because we don't need it in the development environment.
Now, rather than using the terminal from outside, we can use the integrated terminal which VS Code provides us, to open the terminal. Press ctrl + `; this will open the integrated terminal. Now to start the server, let's go inside the API folder using
cd API, and run the command
dotnet run. This will start our server.
As you can see, the server is running on http://localhost:5000, exactly as mentioned in the
launchsettings.json file. To stop this server, you can type
ctrl + c, and we can also see that the Hosting environment is Development.
At the bottom is the root path of our project. let's stop the server by using ctrl and c. Now rather than using
dotnet run command, let's use
dotnet watch run command. This will keep an eye on our projects and will show us the changes without restarting the server.
Let's look at the
Program.cs file first. This will be familiar to you if you have worked with C# before. When we run the command
dotnet watch run, this Main method is the one which gets invoked. Inside this method, it's calling a CreateHostBuilder method which is located below. This method is responsible for hosting our Kestrel web server and hosting on our localhost 5000. This method also adds some default configurations to our project, as you can see in the
ConfigureWebHostDefaults. It uses a Startup class which we are going to see next. It also loads the environment variables that come from the
appsettings.json file or from the
appsettings.development.json file. Let's take a look at them. Our
appsettings.development.json will only run if we are in the development environment. It has some default logging settings. Let's change Microsoft from Warning to Information, as it will help us give the detailed logs. The
appsettings.json file will run regardless of any mode we're in; Development or Production.