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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back in previous session we had seen how to consume the API in asp.net core MVC application how to read the data by using API we have discussed in the previous session in today's session we are going to create a product by using API here there is a hyperlink whenever user clicks on this hyperlink that form should come for the data entry and whenever user submits it should go to the API and it should insert inside the database this is the functionality which we are going to implement in today's session go to the MVC application open the controller this is the controller which we used to consume the API here I am going to create one more method HTTP get and here I will make it as create this method I am going to use it for product creation view purpose right click on this action method click on ADD View here I'm going to select the razor View here I will select the template as create I am going to select model class here that is product view model click on ADD here also we are going to use this cap folding mechanism to generate the views view generated to create a product and here I am going to create another action method to post the data to the API for that I will make it as HTTP post and I am going to create another action method this is also create here we will get the data in the product view model I'll make it as model here I am going to serialize the data into Json format here we will receive the data as model here we are going to serialize into Json format I am going to use the Json convert here series object here I have to pause model here I have to pass the string content to pause data and I have to define the encoding type here and here I have to pause application slash Json we are sending the data in the form of Json here we have to add the required namespace that is system.txt here I am going to get the response from the API here we are going to post the data here again we have to pass Base address as well we have to pause the controller name and action method name we have to check the action method name here open the web API go to the controller so this is the action name we used for post the data we have to pause same thing here so along with this we have to pause the content if status code is Success then we are going to return this to index View I will use the redirect to action here I am going to pause index also here we have to check is there any error here for that I am going to use the try catch if there is any unhandled exceptions are there we can handle easily here I'm going to display this here we have to make it as result we will get it as result so that we can avoid this error also once this data is successfully inserted we have to display the message to user for that I will use stem data I'll make it key value as success message and I'll add it as product created this is the message which I am going to display after the product creation also if there is any error I am going to display that in error message here I will display the exception message these success and error message we are going to display globally for that I am going to use the shade layout.cs HTML and before rendering the body I am going to display this message so I used in my previous examples and we explained already how to use this I am going to copy this content and I am going to reuse there I'll paste it here we have used the same key value as error message and success message here error and success run the application now both our API and MVC applications are running here I am going to access product controller so our product controller is loading and this is the index page I am going to click on create new and we are able to see our form to create the product here we know need of ID so I am going to remove this ID here I'll go to the create view because ID is identity field we now need to insert any values this primary key so this will be incremented automatically and also I want to add top margin here margin top to and run the application once again I'm going to access product controller again click on create new so now ID has been removed here I am going to add one product here and add price and quantity click on create so product is created and it is added to our list of products here also we can Implement to close this alert automatically that also we can Implement in previous sessions already we implemented that go to the WW root here I have created one custom Js to close the alert automatically after five seconds this is the simple JavaScript function which we are going to implement it here I'll go to the root folder JS here I'm going to create one custom Javascript file we create a new item here say Javascript file I'll make it as custom here I am going to paste this I'll copy it again I'm pasting here so this is we are setting timeout for all the alerts like 5 Seconds Whenever there is a alert is showing after five seconds automatically it will be closed if user wants to close before this 5 Seconds user can close with close button so now this custom.js we have to add inside our layout.cs HTML then only it will be implemented globally I will drag and drop here so this script is added to our layout.cs HTML run the application I'm creating new product here create it so our bootstrap already showing here it should close automatically after five seconds it is working fine like this we can use the bootstrap alert also inside asp.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: 4,863
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, mvc tutorial, web api tutorial .net core, webapi, 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, code with gopi, c#, entity framework, .net core, .net 5 web api crud, mvc, entity framework core, dot net core
Id: pIlG3JVZy1Q
Channel Id: undefined
Length: 10min 24sec (624 seconds)
Published: Wed Mar 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.