ASP.net Core Web API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody welcome to digital tech joint in this demo we are going to show you how to create a web api using asb.net core so let's get started uh we will be showing you demonstrating you how to create a web api that will fetch your record from the database that will perform the crude operation that will create a new data set that will create a new record update a record and a deleted record so let's get started so select asp.net core web application say next uh name it as web api demo whatever you wish to i set the location and i say create [Music] so we select a web api as of now i'll uh and take this configure for https and i say create so while the project is getting created i'll show you what i have created in the database so i am using ms sql database and i have created a table a database has api demo with a table with customers and which already has some set of records so we will be displaying this record in postman using the the web api should be create we'll also add records and delete records from this table so yes the project is created so let's go ahead and add some necessity packages which is required uh while we are working on this demo so let's browse and the first thing is microsoft okay you already have microsoft.ndt framework code select this package and say install i accept and it gets installed uh yes it's finished then we select since we are working on ms sql database we will also require microsoft entity framework and will require dot sql server so install this also accept it great so when these these two packages are required now let's go ahead and create a create a set of the connection string so go to appsetting.json and say connect sorry see connection string enter give it a name i have named it as default connection and and then say server it goes to local database databases api demo already so connection string is set up let's go ahead and add the folder for setting up our dtu class so let's call it as dto so this will hold the properties of the table so we create a dqs customer dto we add in the required properties so we say prop double tap and one more tab and this is our customer id which will be a unique key and then we have prop double tap so we say string tab and this is the customer name the remaining two fields also strings let me just copy this copy this and name it as mobile and email so these are the field matching with the field of the table customer id customer name mobile and email a customer id is a primary key so we will use key as and then i think so this requires a class called data annotation yes so our dto is ready now let's go ahead and use a db contest class so let's create a folder as data and let's add a class our own class as db contest dbe context so db contest will inherit it db context class so we have to maybe if you can't contact so if this is not appearing then i'm sure there's some yes so we will see using entity framework code so now this is inheriting db context so now let's create a constructor a constructor within parameters of options so let's leave this constructor as it is because we are going to inject the connection string from a startup class okay so let's leave it as that as of now let's leave this and then create our table representation db set and this is our customer tto this is a table customers get sent okay so let's pull in this is requiring a reference of dto class so it's over here so we added this reference which is over here okay so we have pretty much finished with our data layer objects and we are finished with our details so now let's go ahead and work on the main controller class now by default we have a controller called weatherforecast.controller let's create one more controller as customer okay we say empty and we say custom so this is our customer class now this class will have api controller and controllers controller okay now by default we have a method called index which returns a view obviously we are going to create our own method which will use the db context class and fetch the data from the table add and update delete records using our methods now let's reference the db context okay now this we need to refer the using demo and let's inject that into our constructor it is never constructed let's create a constructor class generator to create a constructor class yeah so so this is generate constructor okay so see this is automatically generated a constructor and this constructor class is injecting is having a parameters or db context and it is passing to our private variable over here context now let's first create a method to fetch the record so this will be a http get request so let's create http get you can name this let's get all customers and then [Music] and we pass in our customer dto get customers let's pull in our d2 reference so this is great so this is just a single line of code return await and content customers to list let's say something is not coming so my things have missed or some reference let's check it out okay so this is using this reference okay so we got through so this is our get customer method which will get the customers from our database so let's go ahead add a breakpoint and let's run it and see the output so how are we going to test it we will use the postman tool to fetch his record so once the project is up and running we'll open the postman i already created a set of collections uh for the postman but i'll explain to you the process so this is a method which i created and this is a customers class and this is get all customer this is a url and let me and this is important so you are getting a get so you need to select the proper http work and then the url and get okay so the url maybe the url is changed it's okay so the id is changed so let's pull up this and go here okay the port is changed so now this is a url and this is a customer controller and get our customer let's set it okay so there is an error unable to solve okay because because because because i have not yet indeed injected the connection string class so let's go to startup and let's go to services we see services we need to add the necessary classes over here too so we say using microsoft.ntp core and using microsoft ltt dot sql since we have a sql as a database connection we select services dot add db context use your data contest class so i'll just add this reference so it's mvpi.demo.data which is this folder okay and then you say open options [Music] options dot use sql and then you pull in the connection string from your app setting transition so you use this so if you notice there is already a constructor which is passing the configuration i configuration interface we are going to use this variable configuration to pull in the connection string from our app settings.json so we say configurations.configuration.connectionstring we pass in our connection string name that is default connection and i think so that is enough and then you use it so this was missing so we had not uh no established connection okay so it is okay so this is first missing correct so now let's again run the project and this time we should be able to successfully fetch the record so yes it's running and let's pull in fine the debugger was called and now we step in and we are calling this great so we have successfully fetched the records so these are the records now let's go ahead and create methods for adding new records okay so this will be a post request so let's add a http post request so we say http post and name it is add customer and then we create public so this is done now what are we going to say is our new customer here available as customer dto and we say customer name equals to underscore customer dot customer name comma mobile [Music] whatever was passed in this object will be added in the this variable the new variable which we declared and then we simply say contest dot customers dot add and pass in this new [Music] so if you want we can return back since we are using this method and we are passing this customer data we need to return back the dto object so return new customer dto and then this is option if you want to return no content you can do that but the calling method need to know that the records were successfully added and also it needs to fetch in the customer id which is an unique id auto identity to the calling function so that they come to know that the customer is added and they get a unique reference number so this is a good idea that you return back and you say new customer dot customer id and customer name new customer customer name then mobile just a new customer new customer dot [Music] so we have passed all the values back to the calling method that's it so this is our project ready now we will go ahead and also test this so we put a break point we run the project we go we move on to our postman now in post plan i've already added a method called add customer so you can see the url i think so we need to be changed so we just copy again okay the url has changed now here we need to select the body tag and need to pass the variable so customer id will be zero because we are adding a new record and i add a name as chance and change the number and change at the rate order so this is a record set let's go ahead so we call this method yes it's called and then you look at the method c to all the values james and james at the right yahoo.com with the mobile number and we add this or save it and we return it back so we look at postman so postman has returned back and notice that the customer id is seven now let's get back to get customer and call to see whether this was added and see the result you can see that this record was added in database and also go to a table and say select see this is added so we have completed two parts we have finished the calling of records fetching records from web api adding records from web api and now let's do an update so for an update the command is put so http work is put so let's do one thing let's copy this add method and paste it change this to http put not patch put and you see see it has so here we are passing the customer id so what are we doing different over here is that we will pass in the customer id as a parameter which requires an update so we say customer id okay and we say update customer we also pass in the customer id which matches to the fields over here and obviously we pass the rest of the information which needs to be updated okay now what are we going to do is first we'll make sure whatever id is passed is available in the table in the database so we say we had a small check customer id which is pass is not equal available in underscore customers dot customer id then we say it's a bad request we say return request so resistance return that request now what we need to do is that simply we need to call the contest class dot entry pass in the object dt object set the state as entity state dot modified now we see we just delete this because we are not adding an account and we say await contest save changes sync now we are not going to return it back if you want we can do it but we say return no content that's it let's also add the relevant methods for deleting the record and test both together so we copy this this will be in a similar line and we say http delete we pass in the customer id we just change this to delete customer we do the same check over here but instead of modified we say delete it that's it these are the two things we need to do and then we let's test it so we run the application so what i will do what we do is that we will first fetch the record so okay so change so what we'll do for this record which we added james we will change their email address from yahoo to gmail so the ide is seven so what we do is that we call update url this day we change this okay called update url we're passing this correct url we say customer and this is seven and most important we say put variable remember this to change it and pass in seven and then we say this was james and then the number was just modifying the number also and we say james games gmail.com so let's go ahead and update it okay so there is an error in our exception so what is the exception exception is customer let's see what's errors let's go ahead add a breakpoint over here you see update customer and it's a put customer id which is a seven okay and we are seeing a single method debugger is called now let us check whether the values are passed properly yes it is passed and the customer id is also available so this is a modifier state and we say save stuck up over here exception string of binary could not be truncate the statement okay so wait two minutes so i guess i have understood the issue let me go to the body and smaller minus less possible okay so okay sorry it's not okay now yes it's got updated okay so now if you go to get customers and you can see the customer chains its email address got updated even the number got updated because i had set a uh limit in the database it was exceeding the limit that is why we are getting that target issue okay so we have done with update and now let's go to delete now uh let's change this url back and let's delete that record which you just added which is seven you need to pass in only the customer id which you need to delete i guess there's some exception over here that request so let's go ahead and check what is the problem over here customer id and let me see so we are passing seven and six oh okay see this is how you add an exception and this is how it works so i did a mistake by passing okay now it should work yes let's go ahead and call get all customers and you will see the record which i had it is no more there so guys this is how i demonstrated you the project i created a web api in asp.net code a simple web api dealing dealing with the crude operations and i showed you how to fetch records from the table update record add record and delete record also demonstrate the use of postman and now we are supposed to call the postman and check the output i hope you like this video and please do subscribe and like this video also and stay tuned because there are a lot of videos which i'm going to release every week on on asp.net and ionic app developments and vb.net all this i plan to release and please do subscribe thank you so much guys thank you
Info
Channel: Digital TECHJOINT
Views: 73
Rating: undefined out of 5
Keywords: asp.net core, asp.net core web api, vs 2019, web api CRUD, working with web api, using Postman, Postman, calling web api in postman
Id: sJ39N5r5iQs
Channel Id: undefined
Length: 26min 16sec (1576 seconds)
Published: Mon Mar 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.