.NET 5 REST API Tutorial: 01 Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a res api allows your app or system to expose its functionality to multiple types of clients both inside and outside of your network including clients across the internet this is what you would use if you wanted to write a program to collect data from say twitter yahoo finance or even nasa if you're looking into building your own dress api and you're considering the net platform for it please stay with me as i show you how to do this end-to-end using the latest innovations provided by net5 i hope you enjoy it in the first part of this tutorial you're going to learn the scenario to be used across the tutorial how to create a net 5 web api project from scratch how to use visual studio code for building and debugging the project we're going to work on how to trust the development certificate installed by.net 5 and that's going to be needed for https access and how to use swagger ui to interact with the api to follow the tutorial you're going to need a few things including the net5 sdk visual studio code and some basic understanding of the c-sharp language now let's talk about the scenario that we're going to be using as a domain for this tutorial so let's imagine that we have some sort of a catalog system and that we have a bunch of items available in it so in this sense uh like i am a video video gamer so i like to think of these items as items that i would use within a video game so items like potions uh swords shields and stuff like that right so that's the system that we have in place it has a catalog and of course we're going to have users that are going to be would like to manage this catalog uh via their their browser right they have a browser they want to manage this catalog of items somehow so and things that they were they would like to do is well uh how are we going to create items in the catalog uh how can we retrieve uh the list of items currently available in this catalog how can we update properties uh of of the items and how can we delete items in this catalog so as it is today we do have the catalog available but we lack or we don't have a way to expose this catalog to the internet so that people can just go ahead and manage it from the browser so that's where we're going to introduce a rest api for this catalog and during this tutorial we're going to see how to build this rest api from scratch using dot net 5. so here we are in visual studio code and the first thing we're going to do is open a brand new terminal and in this terminal we're just going to switch to the directory uh where we're going to create our project and to create a project we're going to be using the ncli uh so to do that we're just going to say dot dot net new and the type of project we want that we want to create for our res api that's going to be web api and the name of the project is going to be catalog hit enter and that creates generates all the files based on the web api template so now i'm going to open that folder that got generated catalog and as you open as usual as you open a.net project in visa studio code it will prompt you to add a few additional files for building and debugging the project so i'm going to say yes and those files get generated under that vs code so on the left side you can actually see all the generated files and let's take a quick look at each of these files as a quick lab around so the first one we're going to look at is the csproc file and this file is used this is called the project file and this is used to declare how we're going to build this project in this case we're saying that we're going to be using the net web sdk to build the project which includes a bunch of tools and tasks to specifically design for web kind of projects the next interesting thing will be the target framework a moniker or target framework which in our case is net five the target framework defines the api surface or what kind of apis are going to be available to your project in this case net 5 is perfectly good for us and and the next thing is going to be a bunch of node packages that we are not going to be diving into right now but those are just dependencies that we already acquired on this project close that and the next father i'm going to take a look is just program cs program cs is what we call the entry point of the application and what this will do is just pretty much erase or stand up the host that i mean the process is going to become the host of our product and it also declares a bunch of defaults and it also sets up what it what we call the startup startup class for our project so let's actually go to startup and see what's going on there uh really the main things in startup are that we have this property called configuration uh that we receive as part of the startup constructor and this you can use anytime you need to read some configuration information from multiple sources like from environment variables or files different kinds of files or a bunch of other places a configuration that you don't want to hard code into your service the next interesting method is configure services and this is the the place where you would register all the services that you're going to be using across your application i will talk about this later on in this video and the last interesting piece is the configure method this is what we configure what we call the uh the pipeline the request pipeline on asp.net and so this just defines a bunch of what we call middlewares which are a additional components that will execute before uh your uh before your your controller or your your code actually executes so each of the each of these can execute uh as a request comes into uh into the asp.net process and from there all the way into when your code executes uh but we're not going to explore in this part in this video um a couple of other things are so we have a weather forecast so this is a model that gets auto generated for the sample application here just have a few very simple properties here and alongside this this model there's a controller now the controller in asp.net is just pretty much the class that handles the routes pretty much the routes that your service exposes right but we're not going to be using this in this video so let's not dive too much into that a few other files upside in json this is where you can declare configuration uh that's going to be that you don't want to use hard code into your into your your program your your source code uh so and right now it just has some configuration for login and the hosts are allowed in the app there's also a variant of updating json which is upsetting development json uh so developing i mean the fact that it that says uh does say upsetting stat development.json means that when we're running in development environment uh these settings will take precedence on top of the app settings json so you could have a bunch of these app settings files for each of your environment like production test integration all of these environments now if we talk about environments uh maybe a good time to take a look at the faucious vs code which are task json and launch json does json is just a file that declares the tasks uh which this is a key concept on visual studio code you can declare tasks and a task is can be a bunch of things and in our case the most interesting task is the fact that we want to just run the net build command so dotnet build which is going to be used for building our code and uh in terms of launch.json this is the file that controls what is going to be launched or executed and when we do like an f5 or when we start debugging the code in this case it's already pointing to the right dll for start to start debugging lastly uh we also have launch settings at json and really the only interesting part here that i'd like to do to take a look is the application url uh here we are defining the urls uh urls in plural for our application uh in which case in this case we're saying we're going to be serving our our server in a localhost 5000 and for the https version we're reserving it in which is going to be the default version uh is going to be 5001. we're also declaring the actual a asp.net core environment environment variable and we're setting it as development and so uh also in business studio code this is not the actual one that's been honored the one that is going to be honored is in launch.json uh as you see over here this is the one that takes precedence to record so so that's good and what we want to do now is actually to test this this project just to make sure that everything is running as expected so what i'm going to do is i'm going to switch to the debug hub over here let me expand this a little bit and so what i'm going to do is just click and start debugging let's see what happens all right so a browser pops up and then if you're getting this page here what it means is pretty normal it means that we don't have the self-signed certificate that comes with net it has not been trusted right and so and there's a very simple way to go around decision and to properly trust the certificate that comes with net so let me actually just close this and stop and let's switch back to the terminal the actually let me open a new terminal here yeah there it is so in order to trust the certificate that comes bundled with the net sdk what you have to do is just type net dev search https trust when you run this you're going to get this pop-up uh asking you to confirm that you actually want to trust exert and i'm going to say yes and then that pretty much should do it so i'm going to run it again let's see what we get all right so yeah so we still are not getting pretty much anything here but we are not seeing any truss issue anymore now if you actually want to see something meaningful here although it doesn't matter too much for us at this point what you can do is just go to swagger and then you're going to get this this nice ui so this is what we call swagger or also a open api specification so this is a component that is bundled now with net5 so you don't have to do anything to make it available as you saw we did nothing so it's just there in the slash swagger url and what this does is allows you to easily describe all the operations all the actions all the routes that are available in your api and allows you to also interact with them easily so for example if i just go to the get route and i click a try it out click execute it will go ahead and run it and we can see already some results for that route and then one thing that i like to do as i work on this product is to uh really not uh open the browser every single time that i run the project uh so let's actually switch a little bit the behavior of bs code so that we'll just keep this window open and then anytime we just hit the 5 for hit run it will not open more windows so to do that i'm going to stop here and we can go to launch the json and close this the only thing that you have to do is just remove the server ready action section here and that's pretty much it if i run again and by the way you can do this by just pressing f5 which i'll do right now five that starts the host as you can see but we did not it did not open any more windows which is fine because we have the browser ready to go right there then the last thing that i like to do in terms of a project setup is to simplify how we build our project in visual studio code so for that let me minimize this and go back to visual studio code stop this close that what i'm going to do is to go to tasks adjacent and the only thing that i'm going to do is to add this little section here under the build task which is uh just the what we call the group which is kind built and it says it's default true what this does is that allows us to more easily build a project so now i can go to let me just save this saved i can go to terminal room build tasks and it immediately builds without that it will just have pop up yet another menu to do the build i can also now do ctrl shift b and it will do the same thing so yes that just speeds up our build situation so that's it for this video in the next one we're going to be introducing the domain entity which is the item for this for this service and also we'll introducing our repository class which is the one that's going to be in charge of both getting and persisting the the items are going to be created across the service so yeah i hope this was useful and uh yeah if you like it please consider hitting the like button and don't forget to subscribe and so that you know when i publish any new videos thanks a lot for watching and i'll see you next time
Info
Channel: Julio Casal
Views: 38,508
Rating: 4.9546099 out of 5
Keywords: .net tutorial, .net 5, .net 5 tutorial, rest api, rest api tutorial, .net rest api, .net rest api tutorial, .net core, c#, c# 9, rest, web api, asp.net, .net, visual studio code, vscode, tutorial, api, microservices, beginner, julio casal, dotnetcore, .net core api, asp.net core, microservice
Id: bgk8N_rx1F4
Channel Id: undefined
Length: 14min 59sec (899 seconds)
Published: Tue Nov 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.