ASP .NET Core Web API + Microsoft SQL CRUD APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's create the database tables required for our app we can see that currently we don't have any tables 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 this table will have two fields one to store an auto-generated table id and another one to store the department name let's insert some records using insert query now let's check the data in our table using select query let us similarly create table to store employee details we will be storing employee id employee name department to which the employee belongs to the date of joining and also the uploaded profile picture file name let's insert a record and check data using select query we have now 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 webhost 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 sql server 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 sql server we need to install a nuget package the package name is system.data.sql client let's also import the namespaces 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 using the given sql connection and command we will execute our query and fill the data into data table using 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 now let's test our api methods we will be using postman to test since we can test only get method in the browser the get method seems to be working let's add another department using post method looks like we are not sending success message after adding let's try again we see that the record is added successfully let's verify this 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 need to make some changes to the delete method to accept the id and url 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 bye now let's test our apis the get method works fine let's test the post method to insert a record bye 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
Info
Channel: Art of Engineer
Views: 13,162
Rating: undefined out of 5
Keywords: c#, .net, .net core, .net core api, .ner core web api, web api, get, put, post, delete, upload file api
Id: TxgL1O0G_Yg
Channel Id: undefined
Length: 24min 43sec (1483 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.