Routing
Learn how ASP.NET Core maps incoming URLs to the controller actions that handle them.
What is Routing?
Routing maps an incoming request's URL and HTTP method to the specific controller action that should handle it.
Attribute Routing
Modern ASP.NET Core primarily uses attribute routing — decorating controllers and actions with route templates directly.
[ApiController][Route("api/[controller]")]public class TasksController : ControllerBase{ [HttpGet] public IActionResult GetAll() { return Ok("All tasks"); }}// [controller] resolves to "Tasks" — so this handles GET /api/tasksRoute Parameters
[HttpGet("{id}")]public IActionResult GetById(int id){ return Ok($"Task #{id}");}// handles GET /api/tasks/5 → id = 5HTTP Verb Attributes
| Attribute | HTTP Method | Typical Use |
|---|---|---|
| [HttpGet] | GET | Retrieve data |
| [HttpPost] | POST | Create new data |
| [HttpPut] | PUT | Replace existing data |
| [HttpPatch] | PATCH | Partially update data |
| [HttpDelete] | DELETE | Remove data |
FAQs
It's a token that automatically resolves to the controller's class name, minus the "Controller" suffix — so TasksController becomes "Tasks".
Summary
- Routing maps a URL + HTTP method to a controller action.
- Attribute routing decorates controllers and actions with route templates.
- HTTP verb attributes (HttpGet, HttpPost, etc.) specify which method each action handles.