ASP.NET Core 5.0 Web API with C# [2021] made easy using a Project - Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the asp.net core web api class this is patrick from patrickvideos.com so what exactly is web api when you create an application you may want different types of uis you may want a browser-based ui a mobile application watch alexa and google devices and even other team projects to access your application so how do you make your application accessible to all these types of uis incomes web api if you use web apis in your application all these front-ends can request and receive the response in json format this is the very basics of web api to have a better understanding let us create a project so let's get started in visual studio let us create a new project and here select asp.net core web application make sure it is c sharp now if you are not able to see this template go to the visual studio installer go to modify and make sure you select asp.net and web development over here and scroll all the way down and select dotnet core cross platform development so here let me select asp.net code web application click on next and let us give a name for our project i'm going to call this bookstore api we will see the meaning of api and all those things later on let us first create a project and then i will explain to you the architecture and the use of the api let us select a location for the project let me click on create here in the drop down select asp.net core 5.0 and select asp.net core web api no authentication and enable open api support click on create now that we have created the project first let us go over here and select web browser as google chrome the web apis work much better in google chrome than in ie now let's just run this application click on debug start without debugging and you can see that the web api works we will learn about what swagger is at a later time but this page proves that the web api works now to understand how this works let us go learn by creating a new controller so here right click on the controllers folder and click on add controller here select api and select api controller empty on add and let us create a controller called values controller and click on add so we now have a controller called values controller in this values controller let us write two methods you can copy paste this code from the attached source code so here you can see that we have two methods one called get which takes in no parameters and another method also called get which takes in one parameter called id both of them if you notice are called http get methods now let's run this application again click on debug start with our debugging and here make sure to go to this url that you have and type in instead of weather forecast type in slash api slash values and you will see these values called values 1 2 3 and 4. so what exactly is happening over here so let's look at the architecture of web api web api all requests go to a controller all requests go to a controller a web api controller and the values are returned back to the ui so now let us go back to our project and see how does this call a controller and how does the controller return the value so when we run this project and we typed in slash api slash values what happened was it called the values controller you can see that this values controller can be called by using this route called api slash whatever the controller name is so since the control name was values controller when you typed in slash api slash values it called this values controller by default whenever you call a controller the get method of the controller gets executed the http get method gets executed so it executed this http get method and it returned these four values that we saw over there we saw these four values because the request went to the controller since we requested the api slash values controller it went here it called the default http get method and returned it back to the ui if you add in one more value and run this and if you try this slash api slash values you will notice that we also see this values file right now let's go click on stop now by default it is calling the weather forecast controller you can see that the weather forecast controller was returning the get method but let us not look into this method is too complex for you to look at it right now but how did it call this weather forecast controller by default in properties if you go to launch settings.json you will notice that the launch url is weather forecast if you change this to api values the launch url by default will go to api values instead of weather forecast let's save and run this now if you run this you can see that by default it goes to slash api values now you may ask like why should i write slash ap values whereas for weather forecast i just have to type in weather forecast the reason is the reason is if you notice in the weather forecast controller the route has only the controller name whereas in the values controller we have it as api slash controller name so weather forecast controller only has the controller name and values as api slash controller name if you want only the controller name to be mentioned you can delete this api slash controller you can leave it at this as only controller name and in launch settings you can simply call the values controller debug start without debugging you see that it works fine you no longer need to use api slash values but let me use api values because i want to know if i am calling a web api or a regular controller so i prefer to call it as api values and i'm going to put the api over here too so now you know that whenever you start your application by default there is a request and that request goes to a controller and the controller's http get method gets executed and it returns a value now that we saw how to call the first get method let us learn how to call the second get method this get method takes in some kind of a parameter so let's run this now if i type in slash values like and say slash phi you can see that it is calling the second get method how does that work now when i type in api slash value slash 5 it comes in search of a http get method but there is only one get method here which takes in some kind of a parameter some kind of a value here you can see that it takes an integer so it knows that it has to call this get method that takes in an id or some kind of a number and then it returns the words the value is appending whatever number you send in if i say slash value slash nine you will notice that the value is nine because it came here the 9 came here and then created the sentence the value is 9 and it returned the value you can even debug this by putting a breakpoint click on debug start debugging and here you can say api slash value slash nine here you can see that it came with the value nine you can just go hover on top of that id or you can right click and click on add watch and you will see that the value is 9 over here step over step over and then you will see that the value is 9 is being displayed right here let's stop this next let us create another folder in this project and let's call this folder as models so we have created a new folder called models in this bookstore api project in this models folder let us add a class and let's call this class as book with these properties make sure to copy paste this from the source code so we now have a book class with these properties now let us create one more controller called the books controller right click on controllers add controller make sure it is an api controller api controller empty click on add and let's call this books controller and click on add so we know that to call this controller we have to say slash api slash books in this books controller let us put a bunch of data we are going to create a list of books so we are going to say make sure using bookstore api dot models make sure to copy paste this data from the source code we're just creating a bunch of books or a list of books and below this list of books we're going to create two methods two http get methods so one method is get all books which simply returns this list of books you can see that right here it returns something called as an action result i enumerable of book so basically it returns a list of books and the second method get book which takes in an id queries this list of books you can see that book start first or default x becomes x dot id whatever id you supply it's going to query this list of books and it's going to check if the book is null it's going to return not found if the book is available it's simply going to return that book as an action result so web apis usually return an action result now you may wonder why does these http get methods return action result whereas in the values controller we simply return the string or an i enumerable of string what is the difference well if you have very simple data to return like i have a string or some integer then you can simply return those data this works fine but if you have some complex returns like i want to give a 404 error i want to give a not found message so in those cases you need to use action result so action result will not only return the data it will also help you give out some messages to the people who are calling your apis so in this case you can see that if the book is not available it will return a not found message else it will return the book so now let's run this debug start without debugging or you can debug this if you want we can call it by saying slash api slash books you can see that it has returned all the json objects it went to the books controller it executed the http get method that takes in no id and it returned the entire list of books if you can't if you want to call the other method you need to supply an id so we're going to say slash api books slash let's say 4 and we got the title 4. if we supply some other number like say 25 we receive the not found status that is the 404 status so all requests go to the controller and depending on what parameters you send it will either call the http get method that takes no parameters or the http get method that takes on some kind of a parameter now let us test this web api controller using postman so we are going to call the web api controller using post map postman is a tool used to test these controllers so let's download postman and use it to download postman search for postman and select download postman let's download the app in postman make sure that in settings over here in the upper right hand side corner select settings and make sure the ssl certificate verification is turned off now let us go back to our project run our project and run the same request using postman so we're going to call this request so let us copy this request go to postman make sure you use the get over here and paste the url and click on send you will notice that the postman made a request to your controller and returned all the values right here as json objects this is a much better display same way if you want to search for one particular book like say book idefy the girls we received another json object for that particular book we can debug this application i'm going to put a break point right here click on debug start debugging and we'll go to postman and we'll click on send right here so it will come here you can see that the number 5 has come right here it is going to search for the book from the list of books right here step over the book is not null and it's going to return this book you can see that the book has all these values right here you can click on continue so all requests so all requests go to the controller it searches depending on what are the parameters in the controller it fetches the value and returns it back to the postman now that we have seen how api controllers get called let us separate this data from the apis we want to bring in another layer called bookstore data which will hold the data and later on we will make this layer connect to the database and we will keep the apis the api controllers separate so we're going to create another project called bookstore.data so here in the solution right click on the solution not on the project you need to right click on the solution and click on add new project here search for class library and select classlibrary.net core c sharp so make sure it is c sharp class library dot net core click on next and let's call this bookstore dot data so we have another project a dotnet class library called bookstore.data let us delete this default class and let us create three new folders in this data layer the folders are going to be called models interfaces and repositories models interfaces repositories first let us move the book.cs this class from the models folder over here to the models folder over there so we can right click click on copy right click on the models and paste it over here once we paste this make sure to change the namespace the namespace now should be bookstore.data.models and over here this book.cs we can i'm going to comment it out you can delete it so i'm going to comment out this book.cs you can delete it you can also exclude it right click exclude from project and if the file disappears you can simply go click on show all files to show the files here as a hidden file i'm going to keep it as included so that you can always see this file right here so in the bookstore.data project we now have the book model class now let us create an interface and let's call this interface as ibook repository this is going to be a public interface and this interface is going to have two methods get all books which returns a list of books and get getbook which takes in an id and returns a book make sure you resolve this using the bookstore.data.models folder just follow along i will explain to you the architecture of this later on now let us create a class to implement this interface add class in the repositories folder i am going to create a class called book repository let's call this public and implements ibook repository make sure you use the use correct using statement and make sure to implement the interface now let's move the data from the books controller to the book repository so i'm going to copy this and have it commented over here and let's go to the book repository and paste the code over here so in the book repository we now have the data so what we're doing is we're taking the data away from the controller and putting it in the bookstore.data layer so that our controllers can be clean and it will only have the api controllers if it wants the data it has to go to the data layer now in the get all books we are going to return the list of books so it's going to be return books so it's going to return the list of books and for the get book by id we need to write a link statement so here you can see that return books dot first or default for first or default to work you need to have using system.link and we have x dot id equal to equal to id so whatever id comes in right here it's going to query it and return that book so now that we have moved our data to its own layer so now that we have moved the data to its own layer it has an interface and implementation of the interface and all those things let's go and make use of this data right here in the bookstore api so in the bookstore api books controller you can see that it no longer understands where this book is coming from because it has no longer a reference to the book class so what we can do is so now we can add the bookstore.data layer as a reference in the bookstore api project so in the dependencies right click on the bookstore api dependencies click on add project reference and select bookstore.data click on ok so we now have the bookstore.data project referred in the bookstore api so now you can go and resolve this book you can see that you can now use using bookstore.data.models but of course it's still not able to understand what is this return books here doing our data is now here in this book repository which is an implementation of ibook repository so we can refer to this particular repository right here in our controller we can do something like this we can say private ibook repository books equal to new equal to new book repository make sure to resolve it equal to new book repository make sure to resolve these so here you can see that we now have books pointing to the book repository class which has the data right here now it is not required to say private ibook repository books equal to new book repository it is not required you can simply say you can even use public over here book repository books equal to new book repository we can do it like this and then later on let me show you the advantage of using ibook repository right here so for now we now have a reference to this class this book repository class which has the data in the books controller make sure to save everything of course now this book repository has the methods to get all the books so we can call this get our books right here in the books controller so now we can say books dot get all books and for the get book with id we can call the we can call the book repositories get book with id so here instead of calling this line we are going to say get book and we're going to pass in the id so this method is now going to call the book repositories get book method which will return a book the book repositories get book method will return a book and if the book is available it will return the book if not it will return not found so now let's test this out debug start with the debugging let's see what the build errors are we need to remove references like bookstore api dot models because we no longer have the models folder having any values right here so let's remove this let's start again and let me go to the postman and query the data it works fine you can see that it works fine we can debug it by going to book repository we can put a break point right here in the books controller let us put a breakpoint over here so first it will call the books controller method and then it will call the book repositories method let's run this debug start debugging and in postman i'm going to call the get method passing a number five so it first goes to the books controller the get book id which in turn calls the book repositories get booked by id the book repository is get book by id which then returns the value back to the controller and back to the ui using the action result so when you look at the architecture right now the postman a request it goes to the controller the controller then calls the bookstore.data layer which has the book repository fetches the data and sends it back to the controller which then sends it back to postman so let's look at the architecture using this picture the postman sends a request to the books controller the books controller http get method has the book repository pointing to this book repository right here so it calls this method from the bookstore.data and sends it back right here to the bookstore api back to postman you
Info
Channel: Patrick WashingtonDC
Views: 48,596
Rating: undefined out of 5
Keywords: Web API, ASP.NET WebAPI, .NET Core, ASP.NET Core Web API, WebAPI
Id: nG3yRTPY5wU
Channel Id: undefined
Length: 28min 21sec (1701 seconds)
Published: Thu Jan 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.