Course Introduction and Project Overview
What's up guys, this is Teddy. Welcome to my YouTube channel. In this video we're going to get started on the .NET Core Web API course. It's going to be aimed at beginners. We are going to be building a stock markets social media platform where you can store stocks and equities, you can comment on your stocks, as well as add stocks to a portfolio in a user-based system. You'll be able to log in. It's going to include security, it's going to include JWT, and best of all, it's all going to be in Visual Studio Code. So we're going to go ahead—no more talking—we're jumping right in.
Download and Install Visual Studio Code
First things first, let's go ahead and download Visual Studio Code. Now if you don't know how to download Visual Studio Code, it's incredibly easy. It's pretty much one click. You go to the top Google bar right here, type in Visual Studio Code, go to the Visual Studio Code website, download for Windows, you're pretty much good to go. There's no further explanation needed because it's literally one click and it's designed to be very, very simple. That is the motto or that is kind of the thing behind Visual Studio Code: it is just a very simple editor.
Install Visual Studio (for .NET Runtime) and Workaround
The next thing that we're going to do is— I know that we're going to be using Visual Studio Code, but here's the thing: if you try to install the .NET runtime, I even had issues installing the .NET runtime if you just install Visual Studio Code by itself. We're not going to be using Visual Studio, but if you install Visual Studio it includes the runtime, it includes everything, and everything is installed in just one little beautiful thing. And you also get Visual Studio down the line as well too, because if you're going to be working on .NET in any capacity eventually you're going to have to use Visual Studio. But we're just going to install Visual Studio as well even though we're not going to be using it.
The first thing that you want to do is you want to type in Visual Studio, not Visual Studio Code, and then we're going to go to Visual Studio. You need to make sure to install the Community version. You could install the Professional version, but it includes a free trial. Community is just free forever and it includes pretty much everything that you need. So we're going to go ahead, we're going to install Visual Studio. Now my version of Visual Studio is already installed, but it's going to look very similar. You're going to probably see a screen like this. Then what's going to happen is that you're going to be brought to a screen that looks like this. For this course, all that you need is this one and this one, and also I don't recommend installing Node because I've installed Node through Visual Studio in the past and it doesn't work well. I've never installed Python through Visual Studio and I honestly recommend just installing it separately. Also if you want to mess around with desktop development, go ahead, feel free to install these. These will also work in Visual Studio Code as well too. So just to install these two up here, my personal opinion; but if you want to go crazy and install more stuff, feel free to go for it. I'm not going to stop you.
Second thing is we need to install SQL Server. The way that you install SQL Server is you type in SQL Server into the Google bar up here, then you go down to the Microsoft—you want to go to the official Microsoft version. Do not install SQL Server on any other website. You'll get taken to all types of crazy third-party download sites, but with SQL Server you install through Microsoft. Do not install anywhere else besides Microsoft. Also I always go with the Express version. There is the Developer version; some people install the Developer version, but the Express version is going to be great for 99% of cases. And also if you need some of the features that the Developer version has that the Express version doesn't have, you can install them later—Microsoft will give you the ability to install them later.
So let's go ahead and install the Express version. It's pretty much going to be, quote unquote, like a fake install because I already have it installed on my computer, but I'm going to walk you through it anyway. What's going to happen is it's going to check, you're going to be brought to a custom—I would probably not do that, I would just do the basic installation. It's going to bring you through it; it's going to ask you where you want to install it, so go ahead and install it. After you get done installing it, what's going to happen is that you're going to be brought to this page right here. The next thing that you want to do is you want to install SSMS. It's going to bring you to a page; you want to go ahead click right here. So once it's downloaded, go ahead and open it, and then you just want to click the install button. So go ahead, install. It's going to load all the packages and it's also going to install with Azure Data Studio. You can also use Azure Data Studio, but we're going to be using SQL Management Studio. And once you get it installed you'll see a screen that looks like this. You can go ahead and close out of it.
Frontend Option and Project Folder Setup
Once you've gotten everything installed, let's talk about something very important before we get started. This course actually has a React front end with it. If you don't want the React front end, if you're not a React person and you're just coming for the API course, just open a random folder and make a junk folder project or junk project folder, put 'fin-shark' in it and create it just by going down here and creating a new folder. Then go inside that new folder and just open it up with Visual Studio Code. It's going to look something like this—it's just going to look brand new.
But if you are coming from React or you want to clone the React project, I'll leave a link down with the GitHub and you can go ahead and clone it. What's going to happen is that your project is going to have a front end in it and this is what the project is going to look like for the rest of the course. But I repeat: you don't need the front end and you don't need this actual homepage PNG right here. It's not required and you do not need the front end in order to complete the API portion of this course. But after we are going to be connecting the API and we are going to be connecting the front end, and I will let you know when that happens. For right now, you do not need the actual front end.
Create New .NET Project and Terminal Tips
In order to create a new .NET project what we need to do is we need to open up Visual Studio Code. Then we need to make sure that we are within the fin-shark folder or whatever folder that you created for your project. All that you're going to do is you're going to type in: dotnet new webapi -o API. Then we are going to type in 'API'. What the -o means is that it's going to actually name the folder. If you just type in 'dotnet new webapi' like that it's going to dump all the contents in a folder and it's not going to look as good, I think. Once you do that you're going to see an API folder show up inside of your project.
Now here's another important fact that could really trip you up: whenever you are actually in the terminal—this is the actual terminal right here—you need to be very cognizant that you are within the right project. So if you are just seeing this, this means that you are not technically in the project; you're just in the folder that you made. You need to make sure that you CD into the actual API and once you do that you are pretty much good to go. Once we've got everything installed and we've got our actual API built, the first thing that we want to do is we want to actually run this thing called 'dotnet watch run'.
Run the API with dotnet watch run and Swagger
dotnet watch run is pretty much like starting the engine of a car. Whenever you buy a new car the first thing that you're probably going to do is start the engine. Let's go ahead and start it to make sure that the engine is actually working. The way that we're going to do that is first of all we're always going to make sure that we are within our API folder, then we're just going to type in 'dotnet watch run'. When we run 'dotnet watch run'—it's kind of a tongue twister—what's going to happen is you're going to see this thing called Swagger. Now Swagger you don't really need to know much about; Swagger is just a standard that is used to actually map API endpoints. Back in the day you actually had to have software to test your API endpoints, but with Swagger everything's built into a browser. It autodetects everything and you will see what a wondrous thing Swagger is because it takes away a lot of the hard work of having to actually test our API. If you got everything correct, if you installed .NET correctly, you're going to see something like this.
Program.cs, API Fundamentals, Extensions, and Closing
The next thing that we need to talk about is our Program.cs file. Our Program.cs file is very important because it's pretty much like the back of our TV. If you think about the back of a TV you really don't mess with the back of the TV that much but it's still very important because it's where things are kind of being plugged in and where certain types of settings are set and it's where you can only mess with some of the very important settings. It's more or less just kind of a fancy interface to plug things in and we're going to clean up our Program.cs file right here. We've got a lot of stuff right here. This is actually a minimal API endpoint; we're going to go ahead and delete this because it's just boilerplate code. We've also got a record right here which we don't really need as well.
This is going to be all of the important code and all of this is what you need. Let's talk a little bit about what's actually going on here: this is a builder. This is going to control things like dependency injection. It's going to provide you with services and various things that you can actually add to your program, almost like a module. This is going to control your actual pipeline. The app is going to control the actual HTTP request pipeline and this is where your middleware is going to be, also various settings that you can configure. And at the very end when this is actually executed the actual server is going to be run because things have to start somewhere and this is technically where the point of order or the Big Bang of your app is going to be.
We need to talk about API fundamentals now. Computers—I say this a lot if you watch a lot of my videos—I say this all the time: computers can only CRUD. Computers can't really do anything else. A computer really at the end of the day is just creating, reading, updating, and deleting data. But it's important to know what a lot of these verbs are because you're going to see things like GET, POST, UPDATE, and DELETE and it's important to be able to mentally map these. Even though these are the exact same thing: a read is a GET, a POST is a CREATE, an UPDATE is an UPDATE, and a DELETE is a DELETE. It's important to be able to map these because most people are familiar with CRUD but a lot of people are not familiar with these API verbs. When you think of POST just remember that a POST is just a CREATE; when you think of a read remember that it's a GET; UPDATE and DELETE are pretty much self-explanatory.
Also we need to go ahead and install all of the extensions. You technically, I guess, don't need these but these are going to make your life a lot easier in the world of Visual Studio Code. First things first, go back into Visual Studio Code. You want to go to your extensions file. The way that you install most of them at one time is you just go to the C# Dev Kit and it's pretty much going to install a lot of this for you. But if you don't want to install C# Dev Kit you could just install C# by itself. Let's go to the .NET extension pack. We also want to install the .NET extension pack and we want to install the NuGet Package Manager because we need to install stuff from NuGet. We also want to install 'NuGet Gallery'. Install Prettier as well too; most people have this already but if you don't have Prettier make sure to install Prettier because it's going to make things look a lot better and it's going to format code for you. Also use the C# extension pack by 'Just Creative'—go ahead and install that and that's going to give the ability to quickly go into here and add a C# file. That's pretty much it. After this we're going to get started on actually building out the API. That was a very long video. Hope that you guys enjoyed this. If you did make sure to smash that like button, make sure to smash that subscribe button, and as always thank you for watching.