LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 5018 min read

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/tasks

Route Parameters

[HttpGet("{id}")]
public IActionResult GetById(int id)
{
return Ok($"Task #{id}");
}
// handles GET /api/tasks/5 → id = 5

HTTP Verb Attributes

AttributeHTTP MethodTypical Use
[HttpGet]GETRetrieve data
[HttpPost]POSTCreate new data
[HttpPut]PUTReplace existing data
[HttpPatch]PATCHPartially update data
[HttpDelete]DELETERemove 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.
Next Lesson →

Controllers