ASP .Net Core Web API + MySQL | CRUD APIs Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's open my sql workbench and start creating the necessary tables in the database i have already procured a mysql server from azure i will be connecting to the same let's first create a database we need two tables for our app one to store department details and another one to store employee details let's start with department table it will have two fields one to store an auto-incremented department id and another one to store the department name let's insert some record into it using insert query let's check the data using select query now let's create the employee table it will have five fields employee id employee name department date of joining which is a date field and finally photo file name which stores the uploaded profile picture file name let's insert a record and check data using select query we have successfully created the database objects needed for our app let's create the.net core web api project let's open up visual studio first click on create a new project choose asp.net web api core choose appropriate folder we might not need https for now now let's take a look at some of the important files all the dependencies and packages needed for our app can be found in the dependencies folder launch settings.json file contains the details of how the project should be started controllers folders contains controllers in which we write our api methods we generally keep the configuration details such as database details in app settings.json the program.cs contains the main program which is the entry point of our app also it creates web host which basically helps the app to listen to http requests the startup class configures all the services needed for our app services are basically reusable components that can be used across our app using dependency injection it also contains the configure method which creates our app's request processing pipeline we need to make a couple of changes in startup class one is to enable the course by default all web api projects come with a security which blocks requests coming from different domains we are basically writing instructions to disable that security and allow the requests to be served we are also making one more change to the serializer class to keep the json serializer as our default we might need to install a nuget package to do this let's import the namespace now let's simply run the project and check if everything works fine we see that the project works as expected and we see the json result from one of the sample controller method now let's create the models needed for our app let's create a folder to add our models let's add the department model department model has two fields department id and department name next let's add the employee model it has five fields employee id employee name department date of joining and photo file name let's start creating the api methods let me first add the my sql database connection string in app settings json file now let's add an empty controller to write our api methods for department details screen to work with my sql we need to install my sql client nuget package to read the connection strings from app settings file we need to make use of dependency injection now let's write an api method to get all data from department table i am writing raw queries here you can choose to write stored procedures or maybe use entity framework for this i will be getting the data into a data table object we can make use of my sql data reader to populate the data into data table object using the given my sequel connection and mysql command we will execute our query and fill the data into data table using my sql data reader finally we will return this data in json format let's test this api method this seems to work let's now add the post method to insert new record into department table we will be sending the department object to our post method in the form body let's also create the put method to update records and finally let's implement the delete method we need to make some changes to the attribute so as to accept the id and url now let's test our api methods let's add another department using post method we see that the record is added successfully let's verify this using get method now let's use the put method to update a record the put method also works as expected now let's check the delete method we see that the delete method also works as expected let's implement the api methods for employee details screen let's add an empty api controller let's copy the contents from department controller and make changes accordingly now let's test our apis the get method works fine let's test the post method to insert a record post method works fine as expected now let's test the put method and update a record and finally let's test the delete method the delete method also works as expected let's now write an api to upload the photos let's create a folder to store photos to be able to use this folder to store and retrieve photos we need to add some instructions in the startup class now let's write the api method i am giving a custom root name for this method and this is a post method if there is any exception while saving file i will simply return the default anonymous picture file name we are going to extract the first file which is attached to form data we need to again use the dependency injection to get the application path to photos folder once the file is saved we will simply return the file name let's now test the api by uploading an image file we see that the upload was successful and we can see the image in the newly created path you
Info
Channel: Art of Engineer
Views: 50,230
Rating: undefined out of 5
Keywords: .net, .net core, web api, rest api, mysql, crud api
Id: Br8H-TuSSO8
Channel Id: undefined
Length: 21min 45sec (1305 seconds)
Published: Mon Jun 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.