Route Prefix in ASP.NET Core Web API Routing

Route Prefix in ASP.NET Core Web API Routing

In this article, I will discuss How to Set Route Prefix in ASP.NET Core Web API Routing with Examples. Please read our previous article about Token Replacement in ASP.NET Core Web API Routing. We will also work with the same application we created in our Routing in ASP.NET Core Web API article.

Route Prefix in ASP.NET Core Web API Routing

In ASP.NET Core Web API, the route prefix defines a common path segment that is included in the URL of all actions in a controller. This can simplify the routing configuration by removing the need to repeatedly specify common route parts in each action within the controller. Let’s understand the need and use of Base Route in ASP.NET Core Web API Routing with an Example. First, modify the EmployeeController class, as shown below.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
    [ApiController]   
    public class EmployeeController : ControllerBase
    {
        [Route("employee/all")]
        [HttpGet]
        public string GetAllEmployees()
        {
            return "Response from GetAllEmployees Method";
        }

        [Route("employee/{Id}")]
        [HttpGet]
        public string GetEmployeeById(int Id)
        {
            return $"Response from GetEmployeeById Method, Id : {Id}";
        }

        [Route("employee/department/{Department}")]
        [HttpGet]
        public string GetDepartmentEmployees(string Department)
        {
            return $"Response from GetDepartmentEmployees Method, Department : {Department}";
        }
    }
}

Now, you can access the three methods above, as shown in the image below.

Route Prefix in ASP.NET Core Web API Attribute Routing with Examples

As you can see in the above example, we are using the Route attributes at the action level to define the routes, and furthermore, all the routes in the EmployeeController start with the same prefix: employee. That means the employee is the common prefix used for all the routes available in the Employee Controller.

Is it not good enough to move the common attribute prefix to the controller level? Yes, we can. The common prefix for all the routes should be placed at the controller level, and the route specific to the action method should be placed at the action method level. In our example, the common prefix, i.e., employee, should be placed at the controller level, and the rest should be placed at the action method level, as shown below.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
    [ApiController]
    [Route("employee")]
    public class EmployeeController : ControllerBase
    {
        [Route("all")]
        [HttpGet]
        public string GetAllEmployees()
        {
            return "Response from GetAllEmployees Method";
        }

        [Route("{Id}")]
        [HttpGet]
        public string GetEmployeeById(int Id)
        {
            return $"Response from GetEmployeeById Method, Id : {Id}";
        }

        [Route("department/{Department}")]
        [HttpGet]
        public string GetDepartmentEmployees(string Department)
        {
            return $"Response from GetDepartmentEmployees Method, Department : {Department}";
        }
    }
}

With the above changes in place, you can access the above three resources as we accessed them in our previous example, as shown in the image below.

need and use of Base Route in ASP.NET Core Web API Routing with an Example

With the above changes in place, we eliminate the need to repeat the common prefix “employee” on each and every controller action method. However, sometimes, we may need to override the common route prefix attribute.

How to Define a Route Prefix?

The route prefix is typically defined using the [Route] attribute at the controller level. This attribute specifies the initial segment(s) of the URI that will be common to all actions within that controller. 

How do you override the Base Route in ASP.NET Core Web API Attribute Routing?

Let’s see an example of overriding the common route prefix or base route in ASP.NET Core Web API Attribute Routing. Currently, our Employee Controller class contains three action methods, all of which start with the same Route Prefix, i.e., employee.

We want to add one action method within the employee controller to return all departments. We want to access this resource using the URL department/all. So, let us first add the following GetAllDepartment method and decorate it with [Route(“department/all”)] Attribute as shown below within the EmployeeController.

[Route("department/all")]
[HttpGet]
public string GetAllDepartment()
{
    return "Response from GetAllDepartment Method";
}

With the above [Route(“department/all”)] attribute on the GetAllDepartment() method, when we navigate to department/all, we will get the following error.

How to override the Base Route in ASP.NET Core Web API Attribute Routing?

But if we navigate to /employee/department/all, we will get the output as expected, as shown in the image below. This is because the [Route(“employee”)] attribute is defined at the Employee Controller.

How to override the Base Route in ASP.NET Core Web API Attribute Routing?

Now, the question that should come to your mind is how to override the Route attribute used in the EmployeeController, which is defined at the controller level.

How do you override the Controller Level Route Attribute at the action method level?

In the ASP.NET Core Application, we can override the Controller level Route Attribute at the action method level using the ~ (tilde) symbol. So, modify the GetAllDepartment action method as follows to use the tilde symbol to override the route defined at the employee controller.

[Route("~/department/all")]
[HttpGet]
public string GetAllDepartment()
{
    return "Response from GetAllDepartment Method";
}

With the above change in place, the GetAllDepartment() action method is now mapped to URI “/department/all” as expected, as shown in the image below.

Route Prefix in ASP.NET Core Web API Attribute Routing

Instead of providing an action-specific route using Route Attribute, we can specify the same using HTTP methods such as HttpGet, HttpPost, HttpPut, and HttpDelete. To better understand, please modify the Employee Controller and check each endpoint. It works as expected.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
    [ApiController]
    [Route("employee")]
    public class EmployeeController : ControllerBase
    {
        [HttpGet("all")]
        public string GetAllEmployees()
        {
            return "Response from GetAllEmployees Method";
        }

        [HttpGet("{Id}")]
        public string GetEmployeeById(int Id)
        {
            return $"Response from GetEmployeeById Method, Id : {Id}";
        }

        [HttpGet("department/{Department}")]
        public string GetDepartmentEmployees(string Department)
        {
            return $"Response from GetDepartmentEmployees Method, Department : {Department}";
        }

        [HttpGet("~/department/all")]
        public string GetAllDepartment()
        {
            return "Response from GetAllDepartment Method";
        }
    }
}
Benefits of Using a Route Prefix in ASP.NET Core Web API:
  • By grouping all actions within a controller under a common route prefix, we clarify the API’s structure. This is beneficial in large projects with multiple controllers.
  • Instead of defining the full path for each action, you only need to specify the part of the route that is unique to that action. This reduces redundancy and inconsistencies in route paths.

In the next article, I will discuss Route Constraints in ASP.NET Core Web API Attribute Routing with Examples. In this article, I try to explain the Route Prefix in ASP.NET Core Web API Attribute Routing with examples. I hope you enjoy this Route Prefix in the ASP.NET Core Web API Attribute Routing article.

Leave a Reply

Your email address will not be published. Required fields are marked *