ASP.NET Core Routing Attributes

In this article, we will cover ASP.NET Core Routing Attributes.

So, Let's get started.

ASP.NET Core Routing

Routing in ASP.NET Core is the process of mapping incoming requests to application logic that resides in controllers and methods. ASP.NET Core maps the incoming request based on the routes that you configure in your application, and for each route, you can set specific configurations, such as default values, message handlers, constraints, and so on.

Types of Routing

  • Convention-Based Routing
  • Attribute-Based Routing.

Convention-Based Routing

The route is determined based on conventions that are defined in route templates that, at runtime, will map requests to controllers and actions (methods).

app.UseRouting();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

Attribute-Based Routing

The route is determined based on attributes that you set on your controllers and methods. These will define the mapping to the controller’s actions.

public class HomeController : Controller
{
    [Route("")]
    [Route("Home")]
    [Route("Home/Index")]
    public string Index()
    {
        return "Index() // Action Method of HomeController";
    }
    [Route("Home/Details/{id}")]
    public string Details(int id)
    {
        return "Details() // Action Method of HomeController, ID Value = " + id;
    }
}
[Route("api/Products")]
[ApiController]
public class ProductsController : Controller
{
}
  • HTTP methods to perform CRUD operations.
    [HttpGet] | [HttpPost] | [HttpPut] | [HttpDelete]
    Example
    [HttpGet]
    public IActionResult GetAll()
    {
    }
    
    // api/employee/GetAll -- get all employees
  • HttpHead specifies that the action should be called for HEAD requests.
    [HttpHead("{id}")]
    public async Task<IActionResult> Get(string id)
    {
    }
    Note: Identical to GET except that server do not return the message body.
     
  • HttpOptions specifies that the action should be called for OPTIONS requests
    [HttpOptions("{id}")]
    public IActionResult Get(int id)
    {
    }
  • HttpPatch specifies that the action should be called for PATCH requests
    [HttpPatch ("{id}")]
    public IActionResult UpdateEmployee(int id)
    {
    }
  • AcceptVerbs specifies that the action should be called for one or more HTTP verbs.
    [AcceptVerbs("GET", "POST")]
    public IHttpActionResult GetEmployees()
    {
    }
  • Route specifies a route template for the controller.
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
    }
  • [HttpGet("{Code}")]  specifies Get request with "code" route parameter.
    [HttpGet("{code}")]
    public IActionResult Get(string code)
    {
    }
    
    // api/employee/vis23g  -- Get Employee by code
    
  • [HttpGet("{id:int}")]  specifies Get request with integer "id" route parameter.
    [HttpGet("{id:int}")]
    public IActionResult Get(int id)
    {
    }
    
    // api/employee/5  -- Get Employee by id
    
  • [HttpGet("{id:length(10)}")]  specifies Get request with 10-character long "id" route parameter.
    [HttpGet("{id:length(10)}")]
    public IActionResult Get(long id)
    {
    }
    
    // api/employee/12345  -- Get Employee by EmployeeGlobalCode
    
  • [HttpGet("{id: regex(^\d{{3}}-\d{{2}}-\d{{4}}$)}")]  specifies Get request with "id" route parameter matching a regex.
    [HttpGet("{id: regex(^\d{{3}}-\d{{2}}-\d{{4}}$)}")]
    public IActionResult Get(string id)
    {
    }
    
    // api/employee/5-12-14110-- Get Employee by EmployeeBarCode
    
  • [HttpGet("employee/{department:alpha:minlength (3)}")]  specifies Get request for a 3+ character alphabetic "department" parameter.
    [HttpGet("employee/{department:alpha:minlength (3)}")]
    public IActionResult Get(string department)
    {
    }
    
    // api/employee/support -- Get Employee by department
    

Summary

In this article, I have tried to cover some of the ASP.NET Core Routing Attributes.