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

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
{
}

Summary

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