Getting Started With Web API

Here, we are going to perform one GET operation in Web API, using Visual Studio 2013.

If you want to know the overview of Web API, then please read my previous article on Overview of Web API.

Open the Visual Studio -> Click File from top menu -> New -> Project.



Choose ASP.NET Web Application from New project Window and give the name as “FirstWebAPIApplicaion” and click OK button, as shown below:



Select a template as empty and select Web API from the checkbox given below and then click OK button, as shown below:



We will get the project structure, as shown below:



Now, we will add student model in the model folder, as shown below:

In Solution Explorer, right click on the model folder -> Add - > Class



Enter the class name as Student.cs in “Add New Item” dialog box and click Add, as shown below:



Write the following property in the Student class, as shown below:

  1. public class Student  
  2. {  
  3.    public int Id { getset; }  
  4.    public string Name { getset; }  
  5.    public string FatherName { getset; }  
  6. }  
Now, we will add the controller to handle the http requests.

In Solution Explorer, right click in Controller folder -> Add -> Controller.



In the Add Scaffold dialog box, choose Web API 2 Controller – Empty and click Add button, as shown below:



In the Add Controller dialog box, enter the name as StudentController and click Add button, as shown below:



Now, in the StudentController page, we are going to create an Action method for http GET request.

First, we will add the model class in the top of the page, as shown below:
  1. using FirstWebAPIApplication.Models;  
Thus, we now want to get student details using http GET request, so we have to create an Action for getting the student details from the StudentController, as shown below:
  1. public class StudentController: ApiController  
  2. {  
  3.     public Student GetStudentDetails()  
  4.     {  
  5.         Student student = new Student();  
  6.         student.Id = 001;  
  7.         student.Name = "student1";  
  8.         student.FatherName = "father1";  
  9.         return student;  
  10.     }  
  11. }  
Now, we are able to call this Action and we will get the response back with student details.

For calling the Action, by default, we are using the following syntax.

“YourServiceURL/api/{controller}/{id}"

ID is optional but we can change it as per our requirement by using routing in Web API. If you want to know more details about routing in the web, please read my article on Routing in Web API.

I am using fiddler to show the result, given below:

result

In this example, I showed the GET request only but with GET request we can also use POST, UPDATE and DELETE.

We are using action names starting with GET prefix for Get request. It’s also applicable for remaining verbs, as shown below:

PostActionName, PutActionName, DeleteActionName.

As per our requirement, we can change the name of the action and we can specify the Action verb type and we will use an attribute, just before the action name.

[HttpGet] [HttpPost] [HttpPut][HttpDelete]



In this easy way, we can use Web API for restful services.

Up Next
    Ebook Download
    View all
    Learn
    View all