Routing - Mapping URLs to Code
Level: Beginner
Routing connects URLs to code. Learn this before controllers, actions, model binding, and Web API endpoints.
- Routing purpose: Match URL pattern + HTTP method to C# handler code
MapGet("/students", ...): Handle GET requests to /students (list all)MapPost("/students", ...): Handle POST requests to /students (create new)MapPut("/students/{id}", ...): Handle PUT requests to /students/101 (update one)MapDelete("/students/{id}", ...): Handle DELETE requests to /students/101 (delete one)- Route parameters:
{id}captures value from URL (GET /students/101 → id=101) - Route constraints:
{id:int}only matches integer (/students/abc won't match) - Query string vs route:
/students/101is route (identifies resource),/students?class=10Ais query (filter) - REST conventions: Use nouns (/students), not verbs (/getStudents). HTTP method implies action
- School Management routes: GET /students, POST /students, GET /classes/:classId/students, POST /attendance, GET /reports/fees
- Multiple parameters: Route can capture className and studentId from URL path segments
- Optional parameters: Route parameters can be optional using route constraints for pagination
- Debugging routing: Catch-all routes show unmatched URL patterns for troubleshooting
What is Routing?
Routing is how ASP.NET Core decides which C# code should run for a URL.
Example:
GET /students/101
Routing matches this to:
app.MapGet("/students/{id}", (int id) => ...);
Then ASP.NET Core passes:
id = 101
Basic Routes
| URL | Handler |
|---|---|
/ | Home route |
/students | Students route |
/teachers | Teachers route |
Route Parameters
Route parameters capture values from the URL.
app.MapGet("/students/{id}", (int id) =>
{
return $"Student id is {id}";
});
Request:
/students/101
Result:
id = 101
Multiple Parameters
app.MapGet("/classes/{className}/students/{studentId}",
(string className, int studentId) =>
{
return $"Class: {className}, Student: {studentId}";
});
Request:
/classes/10A/students/101
Result:
className = 10A
studentId = 101
HTTP Methods and Routes
The same URL can behave differently based on HTTP method.
| Method | Route | Meaning |
|---|---|---|
| GET | /students | Read students |
| POST | /students | Create student |
| GET | /students/101 | Read one student |
| PUT | /students/101 | Update student |
| DELETE | /students/101 | Delete student |
app.MapGet("/students", () => "Get all students");
app.MapPost("/students", () => "Create student");
app.MapGet("/students/{id}", (int id) => $"Get student {id}");
app.MapPut("/students/{id}", (int id) => $"Update student {id}");
app.MapDelete("/students/{id}", (int id) => $"Delete student {id}");
Route Constraints
Constraints limit what values a route accepts.
app.MapGet("/students/{id:int}", (int id) =>
{
return $"Student {id}";
});
This matches:
/students/101
This does not match:
/students/abc
Common constraints:
| Constraint | Example | Meaning |
|---|---|---|
int | {id:int} | Must be integer |
guid | {id:guid} | Must be GUID |
min | {id:min(1)} | Minimum value |
alpha | {name:alpha} | Letters only |
Query String vs Route Parameter
Route parameter:
/students/101
Use when the value identifies a specific resource.
Query string:
/students?className=10A&status=active
Use for filters, sorting, searching, and paging.
Example:
app.MapGet("/students", (string? className, string? status) =>
{
return $"Class: {className}, Status: {status}";
});
School Management Route Examples
| Feature | Route |
|---|---|
| Get all students | GET /students |
| Get one student | GET /students/{id} |
| Add student | POST /students |
| Update student | PUT /students/{id} |
| Get class students | GET /classes/{classId}/students |
| Mark attendance | POST /attendance |
| Get monthly report | GET /reports/monthly-fees?month=5&year=2026 |
Good Route Design
Prefer nouns:
/students
/teachers
/attendance
/reports
Avoid action-heavy names:
/getStudents
/createStudent
/deleteStudentById
HTTP method already tells the action.
Minimal API Routing vs Controller Routing
Minimal API:
app.MapGet("/students/{id}", (int id) => $"Student {id}");
Controller route:
[ApiController]
[Route("api/students")]
public class StudentsController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetStudent(int id)
{
return Ok($"Student {id}");
}
}
Both use routing. Controllers organize routes better in larger projects.
Common Beginner Mistakes
| Mistake | Better Approach |
|---|---|
/getStudent/101 | GET /students/101 |
/student/create | POST /students |
| Using route values for filters | Use query string for filters |
| No route constraints | Use {id:int} where needed |
| Very long route names | Keep routes readable and resource-based |
| Mixing singular/plural randomly | Prefer plural resources like /students |
What to Remember
Routing = URL + HTTP method -> C# handler
Route parameter = value inside path
Query string = filters/search/paging
Constraint = rule for route value
Q: How does routing work in ASP.NET Core?
Good Answer: "Routing maps an incoming HTTP request to the correct endpoint or controller action. ASP.NET Core checks the HTTP method and URL path against route patterns like /students/{id}. Values in the URL can be captured as route parameters and passed to the handler. Routes can also use constraints such as {id:int}. In Web API design, routes usually represent resources, while HTTP methods represent actions."
Practice Before Next Article
Create routes for:
- Get all teachers.
- Get teacher by id.
- Get students in a class.
- Mark attendance.
- Get fee report by month and year using query string.
Use ChatGPT, Claude, or Copilot to go deeper on ASP.NET Core routing. Try these prompts:
"Create REST-style routes for a School Management System""Explain route parameters vs query strings""What are route constraints?""Review these routes and make them REST friendly"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.