How does the Routing work in ASP.NET Core Web API?- Part-1

Routing in ASP.NET Core Web API application is the process of mapping the incoming HTTP Request (URL) to a particular resource i.e. controller action method. 

For the Routing Concept in ASP.NET Core Web API, we generally set some URLs for each resource. When we run the application, then it will create the Route table and the Route table will contain the mapping information between the URL and the Resource. So, when we are sending a request from the client to the server, then the application will check the URL in the Route table and if it found an exact, then the application will forward the request to that particular resource else it will throw an error saying resource not found.

We can access any resource using a unique URL in ASP.NET Core Web API Application. It is also possible that a resource can have multiple unique URLs. But multiple resources can not have the same URL and if you do so, then the application gets confused to invoke which action method and as a result, you will get an ambiguity error.

So, the ASP.NET Core Framework maps the incoming HTTP Requests i.e. URLs to the action methods of Controllers based on the routes that are configured for your application. In ASP.NET Core, it is also possible to configure multiple routes, and also it is possible to set some specific configurations such as default values, constraints, message handlers, etc for each route. If this is not clear at the moment then don’t worry, we will discuss each and everything with examples.

Now i will show some method, how we will implement routing in api:

Method_01:

namespace RootCRUDAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public string GetName()
        {
            return "Test Name";
        }

    }
}

First of all, here i created a controller named ValuesController which inherited Controller Base class. I wrote a method named GetName() which return string value. If i wanted to get the values then i should call this method this way: {BaseURL/api/controllerName}. The final URL will be :

N.B: Please note that, when we write any controller name, then we will remove Controller keyword from controller name.

https://localhost:7222/api/Values

Method_02:

namespace RootCRUDAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [Route("getName")]
        [HttpGet]
        public string GetName()
        {
            return "Test Name";
        }

        [Route("getFullName")]
        [HttpGet]
        public string GetFullName()
        {
            return "Full Test Name";
        }
    }
}

If we need multiple method and we write routing previous way, then our program will throw an error. Because our program won’t recognize actually which method will be execute. This way we can set unique routing name above the method.The final URL will be:

https://localhost:7222/api/Values/getName
https://localhost:7222/api/Values/getFullName

Method_03:

namespace RootCRUDAPI.Controllers
{
    [Route("api/[controller]/[action]")]

    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public string GetName()
        {
            return "Test Name";
        }

        [HttpGet]
        public string GetFullName()
        {
            return "Full Test Name";
        }
    }
}

Above this routing [action] section will be method name. When we will write any mehtod then automatically it will take this name. We don’t need to write method name again and again. Now the URL will be this type:

https://localhost:7222/api/Values/GetName
https://localhost:7222/api/Values/GetFullName

173 thoughts on “How does the Routing work in ASP.NET Core Web API?- Part-1

Leave a Reply