This blog post will demonstrate on how to create a RESTful web service using Web API, which is introduced by Microsoft with the MVC 4 release.
I’m using Visual Studio 2012 for this demonstration.
First I will create a New Project and I will select a MVC 4 Web Application. I name it as WebAPIDemo.
But for this demonstration I will not be using the Web API template. I will use the Basic template. I will not be testing the Web API using a MVC application. I will show you some tools available in which RESTful services can be tested.
So next I will create a controller and will name at as StudentController. From the template I will select “Empty API Controller”.
CRUD Operation
|
Method Name
|
Insert
|
InsertStudentDetails
|
Delete
|
DeleteStudentDetails
|
Update
|
UpdateStudentDetails
|
Select
|
SelectStudentDetails
|
GetAllStudents
|
I will
create a class called Student which has four attributes. Place the Student
class inside the Models folder.
Request URL: http://localhost:3625/api/Student/GetAllStudents
Note: You need to change the port number accordingly when you try this out in your machine.
Request:
The response contains the three student details I hardcoded in the list.
Next I will implement the SelectStudentDetails method which accepts the studentID as the parameter.
Request URL: http://localhost:3625/api/Student/SelectStudentDetails?studentID=1
Here when invoking the method the studentID is passed.
Response:
For rest of the demo please read INTRODUCTION TO WEB API (PART 2)
Here when invoking the method the studentID is passed.
Response:
For rest of the demo please read INTRODUCTION TO WEB API (PART 2)