Reviewing the Project Files

In this lesson, we'll go through the entire project that comes with Web API project and Class library Project

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:07] Our projects are now ready for the action. And what you need to do is open the IDE or code editor of your choice.

    [00:08 - 00:19] I'm using VS code, so let's open the Learnify project. I will type code followed by a space and then I'll press command to open the project directly from the terminal.

    [00:20 - 00:32] If you want to do the same, open the command palette using command shift P on Mac and control shift P on Windows and type shell. And choose this option.

    [00:33 - 00:38] The command will be added automatically to the path. Now try to open it once again.

    [00:39 - 00:45] Code and command press enter. Now as you can see the project is here.

    [00:46 - 00:57] Now you're ready to open any project from your terminal. Once you've opened the project in VS code, you will get a little pop up asking required assets to build and debug are missing from Learnify.

    [00:58 - 01:04] Add them. This notification is from the C# extension and we want to add this.

    [01:05 - 01:09] So press yes. If you press not now already, don't worry.

    [01:10 - 01:18] Open the command palette once again and type generate. And select this option generate assets for build and debug.

    [01:19 - 01:28] So I already have it, so I will cancel it, but you can select yes. This directory contains launch.json and tasks.json file.

    [01:29 - 01:40] We will talk about them once we need them. As you can see, we have three directories API, entity and infrastructure and a solution file, Learnify.slm.

    [01:41 - 01:45] Let's look at Learnify.slm first. It's the structure file for organizing projects.

    [01:46 - 01:54] It contains text-based information about the project environment and project state. For example, if you search for API, there you have it.

    [01:55 - 02:04] It has all our projects and it manages the state of our projects. Inside the infrastructure directory and entity directory, there are two files and two folders each.

    [02:05 - 02:13] One is a CSproj file, which takes care of all the dependencies. It also tells us the SDK version we are using.

    [02:14 - 02:24] And it also has information about the references that we have added in the last lecture. They also contain class 1.cs files, which we don't need, so we can just delete them.

    [02:25 - 02:31] 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.

    [02:32 - 02:50] If you are seeing them and want to get rid of them, open Settings and type "Ex clude". Click the Add button and add star star slash pin and star star slash object and press OK.

    [02:51 - 03:00] I have excluded them because they appear every time you create a new .NET project and you have to manually delete them every single time. This process takes care of the manual deletion work.

    [03:01 - 03:11] 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.

    [03:12 - 03:21] Every request will first hit this project before going anywhere else. Inside the properties folder, there is a launch settings.json file.

    [03:22 - 03:31] 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.

    [03:32 - 03:44] And depending on which IDE we are using, different profiles will be invoked. If you are using Visual Studio, it is going to invoke the IIS express, which will start the server on the random URL.

    [03:45 - 04:02] For us, if you are using Visual Studio Code, it will use the API object, which will run a server on HTTP server 5000 and HTTPS server 50001. By default, it launches the browser when the server starts.

    [04:03 - 04:08] I find it annoying, so I will turn it to false. You can choose as per your convenience.

    [04:09 - 04:20] By the way, it uses swagger on the browser to list our API endpoints. If you don't know what swagger is, swagger allows us to describe the structure of your API so that machines can read them.

    [04:21 - 04:32] Let's get rid of the HTTPS endpoint because we don't need it in the development process. Now, rather than using the terminal from outside, we can use the integrated terminal, which VS Code provides us.

    [04:33 - 04:40] To open the terminal, press control and apostrophe, which is below the escape key. This will open the integrated terminal.

    [04:41 - 04:53] Now, to start the server, let's go inside the API folder using CD API command and run the command .NET run. This will start our server.

    [04:54 - 05:01] As you can see, the server is running on HTTP localhost 5000. Exactly as mentioned in the launch settings.json file.

    [05:02 - 05:09] To stop the server, you can type control C. You can also see that the hosting environment is development.

    [05:10 - 05:18] At the bottom is the root path, which is users, my name, desktop, learnify, and then API folder. Let's stop the server by using control C.

    [05:19 - 05:30] Now, rather than using .NET run command, let's use .NET watchrun command. This will keep an eye on our projects and will show us the changes without restarting the server.

    [05:31 - 05:37] Let's look at the program.cs file. This will be familiar to you if you have worked with C# before.

    [05:38 - 05:49] When we run the command .NET watchrun, this main method is invoked. Inside this method, it's calling a createhost builder method, which is located below.

    [05:50 - 06:00] This method is responsible for hosting our Castile web server and hosting it on our localhost 5000. This method also adds some default configurations to our project.

    [06:01 - 06:16] As you can see inside the configure web host defaults, 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.

    [06:17 - 06:24] Let's take a look at them. Our appsettings.development.json file will only run if we run in the development environment.

    [06:25 - 06:32] It has some default logging settings. Let's change Microsoft from warning to information.

    [06:33 - 06:42] It will help us giving the detailed logs. The appsettings.json file will run regardless of any mode we are in, development or production.

    [06:43 - 06:53] Now, let's take a look at the startup.cs file. First of all, we have the startup constructor which takes as parameter iconfiguration.

    [06:54 - 07:08] What we are achieving by doing this is we are injecting the configuration inside our startup class. The iconfiguration parameter will give us access to the variables that we are using inside appsettings.development or appsettings.json file.

    [07:09 - 07:20] Our appsettings files are nothing but key value pairs of information. The configure service method adds services to the container such as the token services or the email services.

    [07:21 - 07:33] Currently it has the add controller service because of which a project can take and respond to the HTTP requests. It also has a swagger service which is also giving shape and documentation to our APIs.

    [07:34 - 07:44] Although we are not going to use swagger intensively, we will use it briefly later on. Coming back to the method, use configure services method to add dependency injection to your project.

    [07:45 - 07:52] Now, let's talk about the configure method. It says, use this method to configure the HTTP request pipeline.

    [07:53 - 08:07] Since we are making our web application, job of our application is to take HTTP requests and sending back the responses to the client. Handling HTTP request is a process which is sequential and that's why it's called a pipeline.

    [08:08 - 08:20] So if you want to add any middleware inside the HTTP pipeline such as cause or redirection, correct sequence needs to be followed. So in short, we will be adding our middleware inside the configure method.

    [08:21 - 08:33] We already have few of them here. This one checks if we are in the development process and run these commands only then such as swagger and use developer exception page which sends us the page as an error response.

    [08:34 - 08:46] Down bottom, we have HTTPS redirection, which we won't use since we are not using HTTPS in development. It also has app.use routing, which takes care of all the routing inside our project.

    [08:47 - 08:53] We have app.use authorization, which we'll use later on. And finally, we have app.use endpoints.

    [08:54 - 08:59] This middleware will direct our request to our endpoints. So this was a brief walkthrough of our projects.

    [09:00 - 09:06] I don't expect you to understand everything at once. It took me a lot of time understanding the purpose of every element.

    [09:07 - 09:17] But we are going to use these files again and again, which will help you to get used to the interface. Now, before ending this lesson, let's check if our server is running in the browser.

    [09:18 - 09:23] Open your browser. Go to the URL HTTP localhost 5000.

    [09:24 - 09:26] Oh, there's nothing in here. We don't see anything.

    [09:27 - 09:31] Don't worry. We are listening on HTTP localhost 5000/swagger.

    [09:32 - 09:38] As you can see, there is a demo API. Let's check it out.

    [09:39 - 09:44] Click on get and then click on try it out. Finally, click on execute.

    [09:45 - 09:50] This will send requests to the weather forecast endpoint and send back a response, which you can see here. All right.

    [09:51 - 09:58] That was a lot to absorb. Take a coffee break and come back where we will see our controllers in the project.