Curriculum
API Routing, Parameters, and Model Binding in ASP.NET Core are core concepts that determine how incoming HTTP requests are handled and processed. Every ASP.NET Core Web API relies on API Routing, Parameters, and Model Binding in ASP.NET Core to identify endpoints, extract request data, and convert incoming values into usable C# objects.
Understanding API Routing, Parameters, and Model Binding in ASP.NET Core is essential because modern APIs must process data from URLs, query strings, request bodies, headers, and forms. These features make ASP.NET Core APIs flexible, scalable, and easy to use.
Routing is the process of matching incoming requests to API endpoints.
Example:
Client Request
↓
Route Matching
↓
Controller Action
↓
Response
Routing determines which code executes when a request arrives.
Without routing:
No Endpoint Selection
No Request Processing
No API Communication
Routing is the foundation of every Web API.
URL:
/api/students
Route:
[Route(
"api/students")]
The request is mapped to the correct controller.
Example:
/api/students/1
Components:
api
students
1
Each segment provides information.
ASP.NET Core commonly uses:
[Route()]
Example:
[ApiController]
[Route(
"api/[controller]")]
public class
StudentController :
ControllerBase
{
}
Attribute Routing provides precise control.
Example:
StudentController
Automatically becomes:
student
Resulting route:
/api/student
This reduces configuration effort.
Example:
[Route(
"api/students")]
Generated endpoint:
/api/students
Templates define URL structures.
Example:
[HttpGet]
Purpose:
GET Requests Only
The route responds only to GET operations.
[HttpGet]
public IActionResult
GetStudents()
{
return Ok();
}
Endpoint:
GET /api/students
Returns student data.
[HttpPost]
public IActionResult
CreateStudent()
{
return Ok();
}
Endpoint:
POST /api/students
Creates a new student.
[HttpPut("{id}")]
public IActionResult
UpdateStudent(
int id)
{
return Ok();
}
Updates an existing record.
[HttpDelete("{id}")]
public IActionResult
DeleteStudent(
int id)
{
return Ok();
}
Deletes the specified resource.
Route Parameters extract values directly from URLs.
Example:
/api/students/5
Value:
5
This value becomes available inside the action method.
[HttpGet("{id}")]
public IActionResult
GetStudent(
int id)
{
return Ok(id);
}
Request:
/api/students/5
Result:
id = 5
ASP.NET Core performs automatic conversion.
Example:
[HttpGet(
"{teacherId}/students/{studentId}")]
Request:
/api/teachers/2/students/10
Values:
teacherId = 2
studentId = 10
Multiple parameters can be extracted.
Query parameters appear after:
?
Example:
/api/students?page=1
Query strings provide additional request information.
[HttpGet]
public IActionResult
GetStudents(
int page)
{
return Ok();
}
Request:
/api/students?page=1
Result:
page = 1
ASP.NET Core binds the value automatically.
Example:
/api/students?
page=1&
size=20
Values:
page = 1
size = 20
Useful for pagination.
Model Binding converts incoming request data into .NET objects.
Example:
JSON Request
↓
C# Object
This process happens automatically.
Without Model Binding:
Manual Parsing
Manual Conversion
Additional Code
With Model Binding:
Automatic Mapping
Cleaner Code
Faster Development
Model Binding improves productivity.
JSON:
{
"name":
"Rahul",
"course":
".NET"
}
ASP.NET Core converts the JSON into a C# object.
public class Student
{
public string Name
{
get;
set;
}
public string Course
{
get;
set;
}
}
The model represents request data.
[HttpPost]
public IActionResult
CreateStudent(
Student student)
{
return Ok();
}
JSON data automatically populates the Student object.
Example:
public IActionResult
CreateStudent(
[FromBody]
Student student)
{
}
Purpose:
Read Data
From Request Body
Commonly used with POST and PUT requests.
Example:
public IActionResult
GetStudent(
[FromRoute]
int id)
{
}
Purpose:
Read Data
From Route
Extracts values from URL segments.
Example:
public IActionResult
GetStudents(
[FromQuery]
int page)
{
}
Purpose:
Read Data
From Query String
Useful for filtering and paging.
Example:
public IActionResult
GetData(
[FromHeader]
string token)
{
}
Purpose:
Read HTTP Header Values
Useful for authentication and metadata.
Model Binding works closely with validation.
Example:
[Required]
public string Name
{
get;
set;
}
Validation ensures data quality.
With:
[ApiController]
ASP.NET Core automatically validates incoming models.
Invalid data returns:
400 Bad Request
This improves API reliability.
Example:
[HttpGet(
"{id:int}")]
Meaning:
Only Integer Values Allowed
Constraints improve route accuracy.
Examples:
int
guid
bool
datetime
These help validate route parameters.
Student Management API:
Routes:
/api/students
/api/students/1
/api/students?page=1
Different routes support different operations.
Routes:
/api/products
/api/products/5
/api/products?
category=Electronics
Routing enables product management.
Routes:
/api/accounts/100
/api/transactions
/api/transfers
Financial systems rely heavily on routing.
Supports multiple endpoint structures.
Reduces coding effort.
Less manual parsing.
Cleaner codebase.
Supports complex APIs.
These benefits are critical in modern applications.
Makes APIs difficult to understand.
Allows invalid data.
Reduces usability.
Can complicate requests.
May introduce routing issues.
Routing maps incoming requests to controller actions.
A value extracted from a URL segment.
A value passed through the query string.
Model Binding converts request data into .NET objects.
Reads data from the request body.
They enable APIs to process requests efficiently and accurately.
Routing matches incoming HTTP requests to controller actions.
A Route Parameter is a value extracted from the URL.
Model Binding automatically converts request data into C# objects.
[FromBody] reads request data from the HTTP body.
[FromQuery] reads values from the query string.
They allow APIs to receive, process, and validate incoming request data efficiently.
WhatsApp us