<<INTRODUCTION TO WEB API (PART 1)
Hi,
In this post I will implement rest of the methods.
Now I have the select methods implemented. Now I will implement the InsertStudentDetails method.
As you can see I have invoked another method to get the student data that is been passed through the request. The method implementation of GetRequestData is shown below.
In this method I’m getting the data in the request as a string. Since I’m passing the data in Json format I need a Json deserializer. For that I’m using the Json framework for .NET which can be downloaded from here: http://james.newtonking.com/json . Then I can convert the data into a Student object. The Json which is passed in the request is shown below.
{
"StudentID" : 4,
"StudentName" : "Nipuni",
"Age" : 35,
"Address" : "Colombo 5"
}
Request URL: http://localhost:3625/api/Student/InsertStudentDetails
Set the content type of the header as application/json.
Add the Json to the body section.
Request:
Response:
Now if the GetAllStudents methods is called you can see the newly inserted record to the List.
Next I will implement the UpdateStudentDetails Method.
This method also uses the GetRequestData Method to get the details of the student that needs to be updated. Then I search for a matching record with the student ID. If a record is found then I update it or else I sent back a response message Not Found.
The why how the message is invoke is the same as the insertStudentDetails Method.
Request URL: http://localhost:3625/api/Student/UpdateStudentDetails
Next I will implement the DeleteStudentDetails method.
I have searched for the student using the student ID, and if it’s found I delete that student and return true or else return false.
Request URL: http://localhost:3625/api/Student/DeleteStudentDetails?studentID=1
Once a delete operation is done if you run the GetAllStudents method you can see whether the student is deleted.
That's all for this blog,
Happy Coding.
No comments:
Post a Comment