ASP.Net Web API Routing

In this blog, we will see about the Routing system in ASP.Net Web API. Routing is one of the important sections in ASP.Net Web API.

What is Routing?

Routing is the process which decides which action and which controller should be called. .NET Web API supports 2 types of routing.

  • Convention-based routing
  • Attribute Routing

Convention-based Routing in ASP.NET Web API

Let’s start with the first type of routing in .NET Web API which is Convention-based routing or basic routing. Once you create a Web API project in Visual Studio, you will find a file called WebApiConfig.cs
This file contains the conventional routing code like this-

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Web API routes
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

In the above code, you can see a method with the name MapHttpRoute. This method has 3 parameters-
1. Route name i.e. DefaultApi
2. Route template
3. Default value. In the above case, the default parameter is id (You do not need to pass it with URI as it is optional.)

Let’s assume that you have routing available as per the above code and you have a Controller name Get
Then below URI could be there to make a request.

/api/Get – Get all records
/api/Get/1 – Get a record where the id is 1

Consider the below Route details-

config.Routes.MapHttpRoute(
                name: "CustomApi",
                routeTemplate: "api/{controller}/{category}/{productname}",
                defaults: new { id = RouteParameter.Optional }
                );

In the above scenario, the Request URI can be like this –

/api/Get/Category1/product1

/api/Get/Category2/product1

/api/Get/Category3/product2

/api/Get/Category1/product2

Attribute Routing in .NET Web API

Another type of routing in .NET Web API is attribute routing. Web API 2 introduces Attribute routing.

It uses [Route()] attributes to define the routes. With the help of Attribute routing, we can apply routing on any controller or action method.

To use attribute routing in .NET Web API, we must enable this in WebAPIConfig.cs file by using config.MapHttpAttributeRoutes() method.
In the below code, the route is defined using an attribute.

[Route("products/{pid}/product")]
        public string Get()
        {
            return "Product Details";
        }

You may define a default controller as well through Routing.

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { controller = "order", id = RouteParameter.Optional }
            );

Hope you like this blog.

Prev Blog- What are Idempotent and Non-Idempotent methods?
Next Blog- Web API Security

Keep following SharePointCafe.Net

You may also like this – ASP.Net Web API Interview Questions and Answers

Leave a Comment

RSS
YouTube
YouTube
Instagram