asp.net core web APIs for beginners | dotnet 7 web apis

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this course we will learn what is rest and rest API need for rest apis key concepts related to rest apis rest request and response structure create basic rest apis with dotnet 7 web apis crud operations with dotnet 7 web apis SQL server and Dapper in order to start this course you need to pre-install visual Studio for Windows the latest version and SQL Server management Studio you should have at least 8 gigabyte of RAM you should have Windows operating system you can use Mac OS also which have visual studio for Mac but I'm not using it for this tutorial so if you are using Mac then you have to follow different steps when creating project and doing all the stuffs in that project you can develop dotted application in Windows Linux or Mac whatever platform you need to choose but difference comes with IDE now the question is what should you already know to get started first thing you need to be good at shisha if you are not good in C sharp then go and learn the language first otherwise there is no point of using a framework which is built on c-sharp and you don't have any knowledge in C sharp next thing you should have a basic understanding of SQL and you should know how to use SQL server and the next skill you need to be aware of debugging skills it would be good if you know how to debug your code if you stuck somewhere in your code in some error and you do not know how to debug your code and how to use breakpoints then you will be frustrated and that's not a good thing so at least you should learn how to debug your code now let's move to the rest and rest API so what is rest rest stands for representational State transfer it is a architectural pattern for Designing Network application patients and the API is created with rest architecture are known as rest apis question comes in mind why do we need rest apis for example you have an e-commerce application which runs on couple of platforms browser-based application and a mobile application browser-based application might be built with JavaScript Frameworks like react angular or view or it can be a server render applications like asp.net MVC or asp.net core MVC mobile application runs on Android and iOS and create it with tech like flutter react native or any other native technology so the question is how everything is going to be connected in a synchronous manner so we will divide our application in two parts front end and back end front end will be our browser-based web application or mobile application and backend will be our rest apis let's take an example suppose you want to display some products in your application front end will make a request to backend and asks hey give me a list of products your backend will respond with some data and now front-end can display that data rest apis are not the only way to achieve that we can use other things like graphql grpc serverless function and terms of more now let's try to understand rest API is more within another example you might be aware of chat GPT if you want to integrate a chat bot with similar features like check DPT in your application then you have two options one create your own chatbot or use an existing features of existing chatbot like chat GPT and second option seems more feasible chat Deputy have the set of apis which you can easily integrate to your application I'm not sponsoring anything here it's just an example I hope you understood my point if you want to give access of your existing application to other applications then you need to create apis for that then any application that have your apis can access your system so in this course we will focus on creating a backend or rest apis so let's talk about few key concepts of rest apis first thing is resource everything is treated as a resource in web API which is uniquely identified with Uris example here products is a resource and it is identified with whole URL next thing is HTTP verbs or methods if you want to perform any action on the resource then we need to specify a method for that we have typically five HTTP verbs get post put patch and delete get method is used for accessing a resource first is get method and it is used to access the resource if you consider this URL with get request as an example then it will return the list of products next is post and it is used to create a new resource example create a new product put is used to update a resource example update the product and Patch is used to update a resource but partially not whole suppose you want to update on the name of the product then you can use the patch method next is delete to delete a resource example delete a product suppose you are requesting for product list from the backend application which will be available at this URL https double slash www.example.com API slash products let's look into the request structure of rest API first HTTP method or verbs since we need a list of products so we will be using get method next is and find our resource is available at this URL and this URL is our endpoint now next thing is header which is a optional one if you want to pass some additional information like if you want to access some authenticated resources then you need to pass a token in a header next thing is body which is also the optional because we pass body in only post and put request since in a in this example we are not making any post or put request then we will not pass the body here now let's talk about the response structure of rest apis first thing is response body we typically get Json document in response but it can be any file like XML or HTML or anything second thing is response header in the response header we get additional information like pagination data or e-tax next thing we get a status codes if we get success response then we typically get two double zero okay status code and here are some other status codes also so first series will be our 2xx successful responses two double zero Okay 201 created 202 accepted 204 no content 206 partial content and here are the redirection responses three double O and here are some client error responses with four x x like four double zero bad request 4014 unauthorized and so on and next series will be our five double x 4 server responses server error responses and examples are 500 internal server error 501 not implemented and so on so now let's create our first application so open your visual studio and in the right section we have these tabs so go here and click on the create a new project now you have many templates here but we need to search here web API and you will see that we have this template asp.net core web API a project template for creating an asp.net core application with an example controller for restful HTTP service so click select this template and click on next now here let's enter a name for application so I'm gonna name it dotnet API demo now let's name it dot API and I'm going to change the name of solution because in future I will add another project to this solution that will be for our database related operations so I'm gonna just change the name of solution so our solution will be dotnet API demo and project name will be dot at API demo dot API so now click on next and here we have multiple sections here so our framework is dot at 7.0 which is latest right now and authenticated will be authentication type will be none configure four https yes check this one out enable Docker leave it as it is now here we have this option use controller uncheck to use minimal API so we are going to create controller based apis here so just keep it checked now enable open eye support open API support so when we enable these options then we have inbuilt Swagger documentation for our project so keep these options checked now click on Create and it will create a project so so our project has been created so let's run this application and let's see how it looks like so by default it is our application how our application looks like so this page is our Swagger documentation page so we will see it about later let's close this thing for now now here in the solution Explorer we will see our application architecture so here we have this program.cs file which is the core of our application so here we have this Builder variable and here we are creating some configuration and this section Builder dot Services dot add controller and this Builder dot Services section here we Define our services so whatever Services we need in our application we will Define it here suppose we need a controller based API so we need to use this service Builder or Services dot add controllers here and these two are also services that we are going to use in our project now we have this section app section and here basically we defines our middlewares so all of these r's are inbuilt middlewares middlewares are just a piece of code or method which sits between request and response so when we make a request it will go through all of these endpoints so first our request will go to this end point then that request will be transformed by this endpoint if it is defined there so then that request will be handled here and it will be passed to the next endpoint then that request will be handled here and then it will be passed to the next endpoint so if it fails anywhere then our request pipeline will be short circuited suppose we have made and request and authorization middleware is applied in our project it means any unauthenticated user cannot cannot make a request so when you make a request without any authentication details then in this endpoint your request will be sort circuited and you will not go further so I hope you are understanding what is the need of end points sorry not end points need of these middle viewers so these are inbuilt middlewares you can create your own middleware's according to your need so it is our program.cs file which is the core of application now we have another file Here app setting dot Json and if you look at here we have another file app setting dot development dot Json so these two files are pretty much similar but there is a difference between both of them so if you have same configuration setting in these two files then in development environment app settings dot development dot Json file will get priority and in server environment or production environment the app setting dot Json settings will be will take priority so it is our app setting.json file here we Define every setting or configuration related to our project suppose we need a connection string to connect with our database so we will Define it here in the app setting dot Json file or app setting.development dot Json file okay now we are done with this and now we have this controller section here we Define our controllers here we have our properties and we have this file launch setting.json and it is basically a launch settings so here you can see that by default we will when we run this application so this URL is going to be our default URL for your application so when we run this application this URL will automatically open or if you are not using https then this URL will be automatically take place now we have this dependency sections and here we have all the packages that is down that comes with this application okay so it is our project and this one this one is our solution right now we have only one project inside this solution but we can have multiple projects inside a single solution so by default you have seen that we have this weather forecast controller so we are not going to look at this instead of that we will create our own controller so now let's create our own controller for that in the controller folder you have to right click so right click here and click on this add section and here click on controller so in the left side we have multiple options MVC controller View etc etc but we have to click on this API section and in this API section you will see that we have various options API controller empty Pi controller with retried actions with using Entity framework with read write endpoints with blah blah blah so we will just create this empty API controller so I'm gonna select this one and click on button add now here we need to select the name for our controller so a controller will be greetings controller let's change it to greeting greeting controller dot CS do not forget to add controller suffix here so our controller name should be here we will have the name then at the last we need to put a string controller so it will be a greeting controller click on ADD and here we have our controller so here it is the name of our controller greeting controller which is inheriting controller base when we put this API controller what we call it ah attribute so we have put a API controller attribute here it means this controller is going to be a API controller so if we remove this attribute from here then this controller will be regarded as MVC controller and here we have the route information so we'll talk about it later let's create our first method here so first we will Define our verb which is HTTP get now public string get greetings and just return hello so when we run this application you can see it here we have new endpoints here API slash greeting which is a type of get so let's click on this so let's expand this thing and click on try this out and click on this execute button now you will see that here in this section it is our request URL let me zoom in so it is our request URL and here it is the response given by the server so in the let section we have the code status code which is two double zero and two double zero means okay and in response body we are getting hello and in response headers we are getting these things content type text plane etc etc date and server casual so it is our response body and from where we are getting this response hello we are getting it from here we have written it from here hello so right now you are seeing that this request URL is this localhost 7223 slash API slash greeting and we can change it also so right now by default it will take a route like API slash and name of controller which is greeting so our default URL will be look like base URL slash API slash greeting which is the name of our controller what if we want our name greetings for that we have to delete this section and let's name it greetings now if we run this application now you can see that we have new endpoint API slash greetings and it was before API slash greeting without any s so that's how we can change our route so it was our get API now we will Define uh post API so it will be HTTP post and here we will have public string post greeting and inside it we will pass response body sorry request body string message and in the written we will return this message so now let's run this application so now you can see that we have two endpoints now in the greeting section one is API slash creating which is a type of get another one is post so let's look at this post now it is asking us parameters message which we have defined here and it is saying that this message will be a query so we are going to pass it from the query for example API slash greeting slash let's just see message equals to hello something like that so we need to pass it like that because here we have defined body sorry we are getting data from the query parameters so let's try this out and let's type something like this hello world let's execute it and in the response you'll see we have two double zero status code which means OK and in response body we are getting hello world which we have passed from here right now it is just a single parameter we can also pass the whole object from here but we will use it later in the next example we are not going to use it here so only a single parameter will be enough for understanding the HTTP post request okay so let's define another another endpoint here which will be HTTP put and let's change its name to put our update greeting and we will return the same message here and let's write another thing here which will be HTTP delete let's change its name to delete greeting and here let's just name it let's assume that we have some greeting ID which we have to pass from here let's simplify it and let's name it to ID and it will return a message greeting deleted now here what we are going to do we are going to pass ID here so let's do it like this now what does it mean if I do not use this or endpoint will look like API oops so power endpoint look like API slash greetings then we will pass then we will pass ID with query string we'll pass it like this so right now our endpoint will look exactly like this so we will change it later but here we need to look at here in the put section here we are updating our greeting right now there is nothing to update it is just for the demo purpose we will we will explain it we will understand it with much more deep examples in the next section when I will create another controller right now this example does not make any sense but it is just for getting started it would be good for the starters to understand the concepts of rest apis so right now we have this put endpoint and here here we will see that we have this thing which is our query string so we need to pass a message so let's name it updated message something like that and let's execute it and you will see that we have updated message here which we have passed in the query string so that is r put endpoint now let's move to the delete endpoint so here let's suppose we have ID3 which we want to delete so let's execute it and you can see here our request URL is like this which I have discussed here okay so what if I want or URL like this oops like this I greetings last one it means I want to delete a resource resource with id1 for that I have to use it like this so let's run this again now you can see that our endpoint has been changed so let's put it here ID3 and here you can see our request URL greeting slash three which is much more clean than before right now we are using swagger UI to test our apis we can also use other softwares like Postman so here in the postman click on the new tab and let's copy this space URL from here and let's paste it here okay now here we will have API slash greetings and it will be get now click on send button and here you will see the response and here in this section you will see the status code two double zero okay same thing you can hear we can change the method from here from this drop down and we're gonna pass let's see what it will need it is it is expecting a string message so just pass it message and hello again it is not the appropriate way of making HTTP post request we always have to pass it from here from the body section it is just for the demo purpose that's why I am doing it here but we will do it properly in the next example so right now here we have status code 200 okay and here we have the response body so that's how we can test our endpoints with Postman now we will move to the next section for that we need to create a database now let's open your SQL Server management studio and let's connect it here now in this section we will create a database so I'm going to create it with queries that feels much more easier so I'm going to create a new database name let's name it dot net API demo press F5 and it will create a database for us now we need to use this database for further operations okay now we are inside this database you can see it here here we can also select it from here rather than using this command okay so now let's create a table so let's create a table create table person it will have ID int primary key identity name in primary key sorry name in not primary key name and where care 30 and it will be not null oops it should be primary key identity next thing we will have email and where care 30 and that's it click on execute button or press F5 and it will create a table for us so let's just write here select a stick from person now you will see that we don't have any record so let's insert some records so insert into person name email use let's name it John John at gmail.com and it should be single quote second will be Mike and Mike at the red gmail.com okay let's execute it and you can see two rows affected so let's execute this command here and we have two records okay that's good for testing now let's move to our Visual Studio section okay to perform database related operations we need to create a new project you can define those things here but I'm going to create a new project for it so right click on this solution and add new project now you will see that we have lots of templates so just search here the class library and select this template click on next let's name it our project was.net API demo let's name it dot data click on next create and it will create a new project for us okay it has created so let's delete this class we do not need it let's close this guy also okay now oops what I have done here I have created this file accidentally let's delete it okay now we need to add a nuget package here so either right click on this project this one and here we have this manage nuget packages we can go here in the browse section and we will select or search a package in this search box it is a one way to do it we also have another way to do it which is directly with the package manager console so we're gonna do it from here because it is beginner friendly here we have this package Dapper by Sam saffron Mark or whatever it is gravel Nick Graber so install this package so it's done now and let's close this guy and let's see how we can see that we have installed this package you can double click on this project and you can see that here we have this package include Dapper version 2.0.151 okay now we need to reference this project to our API project so in in the dotted API dot demo.api project right click here right click on the API project okay not on the data project so right click on the API project here click on ADD and you will find this project reference so click on this project reference select this guy and click ok now we have added the reference of data project to our API project so if we double click on the API project you will see that we have referenced this project okay it means we can access its functionality here otherwise we cannot access its functionality here in the API project okay now let's define our connection string so open the app settings.json file and here just write connection strings and name of an access string will be default here we will Define our connection existing server equals to name of server database equals to name of database then Integrated Security equals to true it means we do not need a password and user ID for connecting to this database and encrypt equals to false makes you to go into few troubles which you will find if you do not include it so encrypts equals to true okay so from where we will get the server and database name so here it is our database name which we have created dotnet API demo so it is our database name so what is the name of our server so let's disconnect this guy and connect it again so here you can see Server types server name so copy the server name of your server okay so my server is my name okay we have created our connection string so let's save this guy okay now here create few folders so first folder will be models another folder will be repositories repositories in the model section we will create a model for our person class so it will be a person dot CS okay make it public so that we can access this class outside this project we will Define few properties here ID name and it will be string email here it will also be string email so we're gonna put some validation logic here so I'm just gonna type here required so now this property will be required and one more thing mean length 30 it will also be 30. so these properties are required and it's minimum length should be 30 sorry not mean length max length maximum length should be 30. that's it now here right click on the repositories add new item and let's add a new class here so it will be person Repository public class person depository and let's create a Constructor here so just type CT or double tap and it will create Constructor for us okay and I'm going to delete these guys we do not need I'm gonna put semicolon here okay now what should we do we have to create few methods here and for that we need few things here first thing we need uh I configuration private read only I configuration config so this thing will be needed for getting connection string from this app setting.json file to access this app setting.json file we have to use this interface I configuration if I put here control Dot then you will see that it is saying that install package Microsoft dot extensions dot configurations dot abstractions so we need to install this package so how I went here I press Ctrl Dot or you can just you can just right here so potential what it was if over your mouse here and click on the so potential features and install this package or just click Control Plus dot or period symbol so I'm gonna put control Dot and install this package find and install latest version so it will automatically install a package for us and it will include it here one more thing private read only a string and connection string so oops connection a string so just type here I configuration config and config equals to config it is called dependency injection we are directly injecting our service to this person repository class now let's see how we can get the value of connection string so connection is string equals to config dot get connection string and value default so how this default I am here so if you have if you remember correctly then we have defined the name of our connection string and its key is default let's go here and you can see that our key is default so you just have to copy this name and paste that name here okay so do not mess with this so it is our key and it is our value our key is default and value is connection string so use this key here okay that's good I have put this question mark here because it is warning me that hey this connection might be empty or null something like that okay now let's create some method here public I think task and let's just name it add person okay in here we'll get this data person oops person okay now we have added this line here person person that's it so how we can save this data to the database so just write here bar connection equals to new SQL connection and this SQL connection press Ctrl dot again we need to install this package Microsoft dot data dot SQL client so find and install this package so it will automatically install this package from the nuget and it will include this thing here so rather we can write here bar or we can specifically Define that I DB connection both are fine and here we need to pass a connection strings so it will be your connection history so I'm gonna put here using keyword it means it will automatically handle the disposability of this connection so it will automatically close the connection and dispose this connection by just putting this using keyword here okay so now just write here a weight because it is async task and connection Dot execute async and here what it is asking command definition so here we will just type strings ql equals to and if we go here and just paste all right that much and our values will be name and email so this name and email we will provide dynamically so we need to define the values for this name and email so these are parameters for our name and email so just Define it here SQL now it is asking us a param so we need to pass the parameters which will be name because we have defined name here and email and how we would get these things so we will simply get these things from here person dot name and person dot email that's it so one more thing here we need to Define command type it will be command type Dot text so we are good here we have successfully added a person so we're gonna do the same thing for update person it is our connection so we will just change the query here take it person 38 person set name equals to name email equals to email where ID equals to ID and here we can pass all the parameters so person dot ID comma person dot name and person dot email because it needs three parameters now name email and ID so that's why we are passing ID name and email so do not worry about the case sensitive because here it is capital n and here it is a small n but it will handle it by itself now we need one more thing here public sink task now we need to return a list of persons so get people okay just gonna copy this line oops just copy this line [Music] a string SQL select asterisk from person now are people equals to a weight connection dot query dot person and here we are just passing this SQL and we do not need to pass any parameters we can pass it here let's just it should be query async so query has sync okay now we are fine so we just have to return this people from here so written people that's good oh we need one more method here which will be get people get person by ID and tidy select a stick from person where ID equals to ID and everything will be same but we need to pass Anonymous object from here so I'm just gonna pass the ID here okay that's good but it is I innumerable and we need to pass a simple object task of person because it is not a list it is a single object so we can write here person first or default that's it and it's saying it can be null so I'm gonna put this question mark here but yeah maybe it will help yeah so we are fine now we need to Define one more method here so it won't return anything it will be just a type of white and let's name it delete person we will pass ID from here so let's name it delete from person we write equals to ID connection Dot execute async SQL and parameter will be ID that's it and we will not return anything from here also so we have created our methods now we need to create interface for this class because we are going to use dependency injection so one way we can create a new interface here and then extend or sorry Implement that interface here but we are not going to do it in this way so here is a pro tip how you can extract interface from a class so here go in the edit section refactor and here we have extract interface so click on this and here you will have these options so I'm not going to select or deselect any options just click on the OK button and you can see that it has inherited this interface and it have created this interface for us so yeah our interface has been created Isn't it nice feature or obviously studio so that's why a full flash ID is useful okay so let's close this guy this guy and minimize this section and move to the EPS section now here in the program.cs file we are going to add these services to our di container so I'm gonna write here Builder Services Dot add transient High person Repository and percent Repository uh service to d i container we are done here so right click here and create a new controller right click on this controller folder and click on controller click on API API controller empty click on ADD button now let's close this guy let's rename it and name it to person controller person controller press add and here we have our controller so let's just remove this and name it to people so what difference does it make so before or end points before or end points will be like API slash person but now let's just see control KC for one minute so now our endpoint will be API slash people and now our endpoints will be API slash people so here we are going to Define get endpoint HTTP get okay public async task and now we will change our return type it will be I X and result and because we want to return we want to return a status code from this method that's why we are using it it means we can return anything from here so that's why we are using accent result before that we were using just a string because in the greeting we want to return the string only but here we will also written a status codes according to our need so two double zero okay standards code will be written for a success message or a successful creation or successful written and other messages other status codes like four double zero will be written for the bad requests okay so let's name it get people okay one more thing let's create a Constructor here because we need to inject our person repository here so I can write here I person repository person repo and if I press Ctrl dot here and Tool Dot and you can see create and assign property create and assign field so click on Create and assign field so it has automatically created a field and assigned it so I'm going to put underscore here so that we can distinguish both now we are good now so here we're going to put a try catch block so that we can handle error also okay so inside the try block we will just write for people equals to a weight and personrepo Dot get people and now here we will return the status code which will be okay it means we are returning two double zero okay from here and inside it we are wrapping this object so inside the two double zero okay code we will also written the people object so it will return to double zero okay status code and people array in response body and here we will just written status code and status codes Dot uh internal server error and we will return a message here to returning a message we will create a new class here so I'm going to create a new class here in the models so add new class response models sorry response model dot CS that's it it will be public and here we will return a status code so prop and status code and prop string message so we can just return a string message from here that something went wrong it is also fine but we will create class and written object of that class here so it is a best practice to do so response model oh what it was so response oops it is it should be response model okay so now we have a good response model press Ctrl Dot and using this so it has added this line here new response model and here we will just pass status code equals to five double zero message equals to something went wrong that's it okay and we can log in this error here also so log ex here but we are not going to do here in this in this example or in this video so I have created a separate video for logging you can check it out in my channel just named exception okay log exception here so let's run our application let's see is it working or not now you can see that we have one more endpoint here this person section and this endpoint API slash people so let's try it out and execute and Hope we don't have any database related error so here we have this error status is five double zero so now you can see that our status code is 500 and our response body status code 500 message something went wrong so now we are going to put breakpoint here and we're gonna catch that exception let's execute it again and let's see here in this exception here a connection was successfully stayed established with server but an error during the login process Okay so let's go here and I guess it should be encrypt false not true so let's try this thing out now let's execute let's try this out execute and now we are good so two double zero okay and here we have our response party so we have added these records earlier in the SQL Server management Studio so that's what we are getting okay so we are very good so far that's a good thing so now we can work on other things so let's create another endpoint here is TTP post this time it will be post public I think task I accent result and just name it create present it will take an object person person now first thing here what we will do check the validation check the validation here so how we will check validation so we have this method model state dot is valid now if it is valid then we will move to this block but if I put this negation sign or exclamatory sign here it means we are not passing a validate valid data so it means validation fate so when valuation will fail we will written a status code a bad request will return a bad request and inside that we will return a new object of response [Music] model status code equals to four double zero and message equals to oops C design teacher so that's fine measure equals to please pass all the required field and valid data so if we per sign valid model then we will return directly from here with four double zero a bad request now if we pass the valid data now we will move further so we'll just write here await person repo dot add person and here we will just pass the person and we will return okay and inside will not pass okay here so we will create we will use another status message created action it will be two zero one here we will pass the name of accent so we're gonna find the name of action with this operator or method whatever it is so we'll just copy this guy and paste it here so it will just written the name of name of this method in a string form and here we can return any object so I'm just gonna return the person which we are receiving from the from the client Okay so I'm gonna use a try catch block here also to catch the exception so try and inside the catch block what we are going to do we are gonna do the same thing so I'm just gonna copy this thing here okay so it is our post request now we will Define here our put request so put request will be similar we're gonna pass here the person object okay and let's name it update person it will be the same and here we will just pass update person that's it and nothing will be changed Okay so here what we will pass here because we are not creating any resource here we are just passing we are just creating the resource so we cannot pass the created action here it does not make any sense so we can just pass okay here or inside the okay we will pass this object now we are going to create another get method here so it will be for get person and here we will pass ID and here we will pass the ID so now let's put a try catch block here so try and let's copy the sketch block catch and what percent equals to person repo dot get person by id id so if person equals to null then what we will return written not found and written not found just pass a new status response sorry response model status code equals to 404 which is status code of not found and message equals to no resource found otherwise we will written okay and here we will return this person so it is a get person so I just forget one thing here in the update what if a person does not exist to update so what we will do we will do the same thing here so I just have copied it from the below so first we will find the existing person okay get present by ID and here we will pass the person dot ID so if existing person he is null so it will return 404 status code along with this object okay so we have defined almost all but we need to Define one more method here which will be HTTP and delete it will be a delete person that's good here first we will check if person exist or not then what we will do here we will just write a weight oops and it should be a weight also wait and same thing will goes here how it and the same thing will goes here where is it oh wait so we have updated this section update person this section we have added a weight and in this section also we have added a weight so now here we will just try it a weight person repo dot delete person and we'll just pass ID so if it's okay then we will just return okay without any any any message or we can just return new response body sorry no response model and status code equals to two double zero and message equals to deleted success fully okay so let's run our application now so here you can see that we have multiple endpoints now so it is our post endpoint so let's try to post the data so let's enter name let's enter just my name and Gmail .com let's execute and let's see so here we have received two zero one status code and here it is the location of our resource and it is the response body so if we look at our post API or post endpoint then we can see that we have just passed this status code created at action and we are passing the same object which we are receiving from here so that's why we have ID 0. and 201 status code so let's see if we pass ah we pass ah invalid model let's suppose we do not pass it let's see what happens so press execute it and let's see so here we can see that four double zero so one or more variation error occurs but but so now let's pass this empty string so you can see that it has written four zero zero status and errors name field required email field is required so it even did not reached here I had put a breakpoint here so it is not even executing our method so let's execute it again and it it did not move inside this method so it means it is not reaching to this section which we have defined it is handling uh error validation automatically so right now our custom validation is not working we need a little bit configuration in order to work with our custom validation so just open let's try to post our invalid data so here try it out and let's remove this guy this guy okay and now you will see that we have our custom error message please pass all required fields and Vary data and here is our Response Code but it does not make any sense because we do not know what valuation error is so for that what we can do we can let's just comment this thing so rather than returning a bad request we will return ah and processed stable entity and here we will pass model state so now we will return four double two status code so now let's let's run this application again and let's execute it and we can see now we have four double two status code and we have our response body here we have name field is required and email field is required so if I pass here a very long string like this let's see what it is saying so now we can see that field name must be a string or array type with maximum length of 30. so that's how our validation works so I'm just gonna copy this line and I'm going to apply same in the put method so here just gonna paste it here now we will test our update persons so I'm gonna just run this application and here in the post sorry not here we will just execute this thing so now let's try to update this record with id2 so we can also use Postman so let's use Postman here so I can demonstrate how we can use Postman for posting data so it would be a put method and our endpoint will be API slash people now here go to the body section and click on this raw and change it to Json object now here let's copy this object and paste it here and let's change it to mic sorry Max from mic so let's try to send it and you can see two double zero status code okay and let's try to execute it again and now you can see that we have Max in id2 and Max gmail.com in id2 okay so that is our put request so so let's copy this guy here and just gonna go here let's change it to delete so let's try to delete with ID3 so first of all let's fetch all the data here so let's fetch all the data with apis less people and here we can see that we have three records here one two three and if I try to fetch with ID3 so here we have this single record okay so now let's try to delete a record with ID3 so I have deleted it and we have this status code here two double zero okay status code two double zero message deleted successfully now if I find this record with ID3 then you can see we have four zero four not found status code and here we have message no resource found and he can also check it here that we do have only two records now Okay so that's how so let's try to delete record 3 again let's see what will happen it will say that no resource found four zero four method so let's try to update record with IDE 3 which is not present now let's see what will happen so here again four zero four not found so and okay so that's it so right now we haven't updated this resource so that's why we are seeing three records here so now we have two records so let's try to add another record here so let's change it to post and let's add this record again and here we will not pass any ID okay now we have created another record so let's see now we have another record with id5 that's it so we can just check it id5 okay so execute it again and now you can see that we have three records so from here you can also get by ID you can use this endpoint so let's see what is in id5 we have this record in ID 5. so let's move here again what if I create another method here as sync task and IX and result just say hello and let's just name it another get and let's name it to http get so now we have one more get method here what will happen so let's see oops but I need to return something from here first of all let's just return okay from here that's it let's see what will happen so here you can see we have error so to fix that we need to Define our own endpoint here so it should be like get what it is another get another get so it is not working as expected another get so I have to remove this Slash from here and just return another get now you can see that we have one more end point here people slash another get so let's try this out and here you'll see another get okay the same thing will happen if I try to add one more post method so with this configuration where we just are using this endpoint then we cannot add multiple get methods so by default this end point will be treated as API slash people by default this endpoint will be treated like this and here we have one more get method but here we are also using IDE here so it is not the same as before so this endpoint will be treated as this like this or sorry not like this it will be treated as like this okay because we are using a parameter here so that's why it is different than before but if we add another get endpoint without any unique parameters then this will still be treated as API slash people now we have this API slash people and this API slash people get request endpoint that's why we need to create a unique name for this adword that's why we have defined it here now our endpoint will be another another get so same thing will happen if I add another post method here so public passing task I action result another post and written okay posted successfully and HTTP post now our endpoint will be API slash people for post and we already have this endpoint by default here so we already have this end point here and we are creating one more endpoint so this endpoint will by default be treated as like this so if we run our application we will see similar kind of situation like we have seen before so let's see what will happen so here we can see that we have this nice error okay so here again we need to Define a name of our method so let's just name it another post now our endpoint will be like this so let's run this guy again and let's see so here let's just see yeah we have another post and point here so let's just try out and we can see posted successfully so that's how we can create multiple get our post or any kind of HTTP verse in a single controller so I hope you understood the situation so now we have successfully created our all the endpoints get post put and delete now you have learned all the important Basics about.net core apis so what next practice these things and make few applications with it once you have better understanding for it you can move to more advanced stuffs like authentication API versioning caching etc etc I have full playlist for that and I will put down the link in the description box you can bookmark it and revisit in the future or you can just note down the topics and learn those topics on your own that's it for this course and all the best for your coding Journey if you want this video helpful then consider to leave a comment or like this video or share this video and make sure to subscribe this channel I will see you next time
Info
Channel: Ravindra Devrani
Views: 2,108
Rating: undefined out of 5
Keywords:
Id: 0T8CAdx3sFs
Channel Id: undefined
Length: 98min 17sec (5897 seconds)
Published: Sun Aug 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.