[Web API] RESTful – Action Binding

The RESTful services maps its resources as URIs and HTTP verbs. Web API can be used as a RESTful service. It uses HTTP verbs as well as action names to find out which controller/action should be invoked upon request.


1. Default Routes

Look at the default route configuration: it does not have {action} parameter or even a default action.

public static void Register(HttpConfiguration config)
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }

So how does Web API find the right action?


2. RESTful (Representational State Transfer)

Web API uses HTTP verbs to specify its CRUD actions:

  • GET : Retrieves resource, safe and idempotent, cacheable
  • POST : Creates a new resource, not idempotent (unsafe)
  • PUT : Updates existing resource, idempotent
  • DELETE : Deletes resource, idempotent


3. Restful By Convention

By default, action names match with the verb names. For example, “GET” request is handled by “Get()” method.

[Note] Action names does not need to be just “Get()” or “Post()”. If names are prefixed with verb names (GetProduct()), actions can be invoked.

public HttpResponseMessage Get(int id)

public HttpResponseMessage Post([FromBody]string value)


4. Verb Attributes

If you want to map HTTP verbs to the non-conventional action names, you need to use the following attributes:

  • AcceptVerbs 
  • HttpGet 
  • HttpPost 
  • HttpPut 
  • HttpDelete

Note that these attributes are defined in the “System.Web.Http” namespaces. When you are working with MVC actions, HTTP verb attributes are located in the “System.Web.Mvc” namespaces. Their class names are the same so be careful with the namespaces.

public HttpResponseMessage Retreive(int id)

public HttpResponseMessage Create([FromBody]string value)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s