Routing in ASP.NET Web API

Introduction

In this article we will define how the ASP.Net Web API transfers the HTTP request to the controller.

Routing Table


We use controllers in the ASP. Net Web API. These controllers work as a class for handling the HTTP request. After receiving the request by the API framework it transfers the request to perform an action. The controller class has public methods; these methods are called action methods.

We use the routing table to determine the action to be performed depending on that action being called. By default the Visual Studio creates a default route for the web API.

public
static void Register(HttpConfiguration config)

        {

            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{Id}",

                defaults: new { Id = RouteParameter.Optional }

            );

        }

We defined this route in the Web.Config.cs file:
 
 config.jpg

We know that the web API is a framework that receives the HTTP request for performing the action. After receiving the request it matches the URI of the route templates to the routing table. If there is not a match then it returns the error to the client. For example, there are various URI matches to the default route:

  • /api/products/
  • api/products/1
  • api/Items/
If there is no API segment then it does not match. For example:
  • /products/1

If the route matching is done then the Web API selects the controller and  action.

  • To find the controller the Web API adds the controller for the value of its variable.
  • To find the action the Web API first sees the HTTP method and the action. It matches the method's begin name with the HTTP method name. For example if there is a Get request, then the Web API sees the action and verifies that the action starts with Get.... such as GetAllItems, GetProducts. And then applies the Get method.
  • And the other variables in the route template work as an action parameter.
Example of the controller:


public
class Items : ApiController

    {

public void GetAllItems() { }

    public IEnumerable<Item> GetItemsById(int Id) { }

    public HttpResponseMessage DeleteItem(int Id){ }

}

Here are some HTTP requests with actions:
 

HTTP Method URI Path Action Parameter
GET api/items GetAllItems none
GET api/items/3 GetItemsById 3
DELETE api/items/3 DeleteItem 3
POST api/items no match  

In this table we note that there is an id segment that represents the id parameter for the action. There are two GET methods, one with a parameter id and another without a parameter.

Routing Variations

Now we will define some variation of the routing table.

HTTP Method

We can specify the method for the action by using the HttpGet, HttpPost, HttpDelete and HttpPut attributes.
Here is an example in which we use the FindItem method to specify the GET request:

public class Items : ApiController
{

[HttpGet]

    public Item FindItem(Id) {}

 }

If you want to allow more than one HTTP method for an action then we use the AcceptVerb attribute, as in the following:

public class Items : ApiController

{

    [AcceptVerbs("GET", "HEAD")]

    public Item FindItem(Id) { }

 

      [AcceptVerbs("MKCOL")]

    public void MakeCollection() { }

}

Routing by Action Name

To determine which action is to be performed, the web API uses a different HTTP method. If you want to create the route, you can create it with the action name included in the URI.

Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{Id}",

                defaults: new { Id = RouteParameter.Optional }

);

The parameter in the route table has the same name as the action name in the controller. Let's see an example as in the following method:

public class Items : ApiController

    {

      [HttpGet]

    public string Details(int Id);

   

}

For overriding the action name we use the ActionName attribute, as in the following:

public class Items : ApiController

    {

       [HttpGet]

    [ActionName("Tulips")]

    public HttpResponseMessage GetTulipsImage(int Id);

 

    [HttpPost]

    [ActionName("Tulips")]

    public void AddTulipsImage(int id);

    }

Non-Action

A non-action is an action that you want to prevent, in other words you do not want to call it. For that we use the NonAction attribute.

public class Items : ApiController

    {

[NonAction]
public string GetPrivateData() { }

Up Next
    Ebook Download
    View all
    Learn
    View all