How Action is Mapped With HTTP Verb in Web API

This article explains an interesting concept of the Web API. If you are new to this concept then I am sure it will provide a little pleasure to you.

We know that the Web API is the latest technology in Microsoft's SOA platform. The beauty of the Web API is that it supports the RESTful service over HTTP, this is the latest buzzword in the industry.

We know that, one of the principals of RESTful services is, it uses a HTTP verb to map a HTTP request to an internal handler, we will see it shortly.

If you are not clear about HTTP verbs then this short paragraph is for you. HTTP is a stateless protocol and it uses verbs like GET, POST, PUT, DELETE and HEADER to perform an action on the Web Server. For example, if we make a request to a Web Server (we might want to access www.google.com) to get a web page then you are making a GET request to the web server in general and that is a normal scenario.

Now, as we have explained, the Web API is the latest service oriented platform, it also supports HTTP verbs to invoke an appropriate action depending on a HTTP request.

Let's see a practical example. We will create a Web API application and we will experiment with the GET HTTP verb.

So create a Web API project and add an API controller to the controller directory of the solution. Please ensure that you are choosing API Controller from the Template List.

API Controller

We have given “person” as the controller name. Let's create it.

There is an interesting rule explaining the choice of Action() under the controller. The rule is that:
  1. At first it searches for Verb name within the controller. For example, if one GET HTTP request arrives at the controller then it searches for a Get() action defined in the controller. If it is present then it executes otherwise proceed to the second point.
  2. It searches for an action that starts with Get. For example if Get() is not present in the controller then it searches for an action that starts with GetXXXXX() for example GetData(). If there is no action that starts with even Get() then please continue to the third point.
  3. It searches for an attribute over action. We can set [HttpGet] or this kind of attribute over action that will help to map that specific action with a HTTP verb.

So, let's prove all the points one by one.

Invoke Action using HTTP verb

Here is our empty controller that contains the Get() method as in the following:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace WebAPI.Controllers  
  8. {  
  9.     public class personController : ApiController  
  10.     {  
  11.         //Get Action  
  12.         public void Get()  
  13.         {  
  14.         }  
  15.     }  
  16. } 

And if we browse for an app/person URL from a browser then we will see that it will stick to the Get() action. Because when we are browsing this URL, we are making a GET request to the service. We see that the control flow has stopped at the breakpoint.

Invoke Action using HTTP verb

Fine, so the first point is clear to us, any Http request is first maped with the same type of verb name from the controller name.

Oh, are you thinking about case? Ok, it does not matter whether it is in upper and lower case.

Let's implement the second point. We will change the action name GetData() from Get(). Here is our modified action in the same controller.

  1. public class personController : ApiController  
  2. {  
  3.     //GetData Action  
  4.     public void GetData()  
  5.     {  
  6.     }  
  7. } 

Now, if we hit the same URL then the GetData() action will be executed automatically.

execute

Now, let's get to our third point. Suppose there is no Get() or GetXXX() in the controller. Have a look at the following code.
  1. public class personController : ApiController  
  2. {  
  3.     //Returndata by turns Get() using attribute  
  4.     [HttpGet]  
  5.     public void RetunrData()  
  6.     {  
  7.     }  
  8. } 

Instead of a GetXXXX() action we defined the ReturnData() action with a [HttpGet] attribute and this will map this action to a GET http request. If we browse for the same URL then we will find that control flow has stopped at the breakpoint.

HttpGet

The mapping process has became simpler by using the HttpGet() attribute.


Similar Articles