How Action is Mapped With HTTP Verb in Web API

This article explains how an action is mapped with a HTTP Verb in the 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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

 

namespace WebAPI.Controllers

{

    public class personController : ApiController

    {

        //Get Action

        public void Get()

        {

        }

    }

}

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.

public class personController : ApiController

{

    //GetData Action

    public void GetData()

    {

    }

}

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.

public class personController : ApiController

{

    //Returndata by turns Get() using attribute

    [HttpGet]

    public void RetunrData()

    {

    }

}

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.