Friday 7 May 2021

Part 7 : Exchanging Data in Web API - 2

# 5 Minutes Of Reading

This is continuation to part 6, in previous article I explained how to pass data from http request using route, query string, request header.
In this article I will show you how to pass data from request body. If you are planning to pass custom type, then request body is used.
Let’s see in action, create Models folder in root directory of project. Under Models folder create User class with few properties shown below.
I'm using in memory data with repository patern, create IUser interface shown below. I will cover repository and dependancy injection in detail in future article, for now just skip repository and dependancy injection or you can visit my dependancy injection article from asp.net core series.
I have created Repository folder and again created two folder inside Repository folder, Interface and Implmentation shown below.
Create UserRepository class under Implementation folder.
Open Startup.cs file and add below highlighted code.
Open UserController and update below code.
Get method of api return all users and Get method with Id parameter return specific user absed on filter, let’s check with postman tool. Below highlighted api/User coming from attribute routing declared at line number 11 above controller name.
Implement Http Post and Put methods, update Iuser and UserRepository class.
Implement Http Post and Put methods, update Iuser and UserRepository class.
Update User controller, add Post and Delete methods.
Run application and check result in postman.
Now delete latest created user with Id 3. I’m passing Id as query string parameter and not route parameter because I didn’t specify Id parameter in HttpDelete attribute.
Now execute Get method to verify record deleted from list of users or not.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home