How To Consume WEB API in ASP.NET Core MVC | ASP.NET Web API | Read Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back in previous session we have created one web API to perform the card operations this is the final output of that session in today's session we are going to consume this API inside our asp.net core MVC application so in this API we have not displayed the action methods we have displayed API slash controller name here we have to add the action name also so that easily we can identify the action method for that I am going to open the controller inside our API project go to the product controller here along with the controller name I am going to display the action also save it I'm running the application again now our API is showing action names also along with the controller name we will check it out here we have two records it's displayed now we are going to create a new project with the asp.net core MVC and we are going to consume this API inside that MVC application this is my solution I am going to right click on the solution I'm going to create a new project here I'm going to use asp.net core web application with model view controller this is my project name I'm going to store in this location I'm going to use the latest.net framework I'm going to create a project now our project has been created here here I am going to create one view model inside these models folder right click on the models I'm going to create a product class here here I have created product view model class I'm going to copy the properties from our API these are all the properties we required if you want you can access this product class by referencing this project to the our MVC project here I am going to make a separate class as product view model I am going to use this properties here I am going to add display name here I am going to add here I will make it as product I'll add the space between product and name here we have to import the namespace this is component model now we have to create a controller to access the API right click on the controllers add the controller here I am going to create one empty controller I'll name it as product controller here we have to initialize our API URL for that I am going to add URI and I'll make it as Base address here we have to pause the our API URL along with the port number this URI we can get it from our API project go to the properties expand the launch settings.json here we are going to use the https I am going to copy this along with this we have to pass the port number which is SSL port number I am copying it here I'll pause it here then slash API this is the base URL of our API if your API is hosted in somewhere in the server then you have to pause that server API address here in our case we have not hosted this API we are going to run from our solution so that's that's why I'm passing as localhost and the SSL port number I am passing here to access this API first we have to fetch the data from our API here we need HTTP client I'll create a Constructor here I'll initialize the HTTP client here I am going to assign the Base address to our HTTP client here we are going to display the list of products this will be get method here I am going to Define list and also I will pass the product view model here product list here we will get the data as HTTP response it will come as message I'll make it as response here we have to pause the Base address and also we have to pause the controller name product is our controller and also we have to pause the action method this is our API controller we have to pause our controller name also action method so I am passing here already we defined the API so we now need to pause this API here I am going to pause Base address along with the controller and action method I'm post here we have to check the response status if it is Success we will get the data in a string format we will get result here now we have to deserialize this string data to Json format here we have to add the required namespace that is newtonsoft.json I am going to install it now here I'm deserializing this string data into list of product view model here I have to pass data here we will get it as result after deserializing these products we have to pause this product list to our view here I am going to create a view now right click on the index action method add View here I am going to click on razor view add here we will select the template as list here we are going to receive list of products and here we have to pause our model class name here I am selecting product view model and click on ADD here we are using this scaffolding to generate our view we are not going to design manually so this Entity framework will generate view for us index view is generated now now we have to test this whether data is binding or not for that we have to run both the applications API and our MVC application for that right click on the solution properties to access the API we need to run the API also here instead of single startup project we have to set as multiple starter projects here select our MVC application also we need to start the API application it is already started apply the changes click on OK save it suppose your API is hosted in the server then you no need to do the settings if it is running from local machine then you have to do this I'll keep debug Point here inside our API controller also from the MVC controller now now we will run the application in debug mode here now two applications are running this is our API and this is our asp.net core MVC application here we are going to access the products list here I am going to call product controller from my MVC application so whenever you are consuming any API that AP should always in the running mode then only you can consume the APO I am going to access the product controller so it's coming into my MVC application index page from here I am passing my Base address to access the product slash get method now it is hitting into AP and it is coming to product controller and here we are getting product list from the API product counties so two records available so it's coming into the else block it is returning here we are going to check the response status it is true here data is coming in Json format here we are deserializing into product V model now we will get as list we will go to The View here we are get two records from the products table now we are going to display inside our view so it is displaying both the records which is available inside the database we will go to the database and we will see how many products available inside the DB this is my SQL Server this is the database which I am using for this EAP I'll execute this select query so we have two records which is displayed inside the MVC application so like this we can consume the API inside esp.net core MVC application that's it for today if you like this video please like And subscribe to my channel thanks for watching
Info
Channel: CodeWithGopi
Views: 21,008
Rating: undefined out of 5
Keywords: .net 5 web api, asp.net core, asp.net core mvc, asp.net core mvc crud, asp.net core web api, asp.net core web api tutorial, codewithgopi, consume web api in asp.net core, consuming web api in mvc, web api tutorial .net core, how to consume web api, how to consume web api in asp.net web application, how to consume web api in asp.net core mvc, how to consume web api in mvc, .net 5 web api crud, .net core, entity framework core, code with gopi, consume web api in mvc application
Id: _TgiJR13cT4
Channel Id: undefined
Length: 12min 54sec (774 seconds)
Published: Tue Mar 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.