IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Synchronous and Asynchronous in .NET Core | Securing ASP.NET Core Web API | |
Model Binding & Validation in ASP.NET Core |
Definition: Model Binding is the ASP.NET Core feature that automatically maps incoming HTTP request data (form fields, query strings, route values, headers, JSON body, etc.) to action method parameters or model object properties.
Instead of manually parsing Request.Form, Request.Query, or Request.Body, ASP.NET Core does the mapping for you.
Sources of data for model binding:
<input> values in POST forms)/products/details/5 → id = 5)?page=2&sort=asc)Example:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class StudentController : Controller
{
[HttpPost]
public IActionResult Save(Student student)
{
// Model binding maps request data to 'student'
return View(student);
}
}
If the form posts Id=1&Name=John&Age=20, ASP.NET Core automatically fills the student object.
Definition: After model binding, ASP.NET Core validates the bound model against rules you define (usually via Data Annotations or custom validation logic).
Validation ensures that the data meets business rules before your action method processes it.
Common validation attributes:
[Required] — field must have a value[StringLength(max)] — limit string length[Range(min, max)] — numeric range[EmailAddress], [Phone], [Url] — format checks[RegularExpression(pattern)] — custom regexExample:
public class Student
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
[StringLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
public string Name { get; set; }
[Range(18, 60, ErrorMessage = "Age must be between 18 and 60")]
public int Age { get; set; }
}
Flow:
ModelState.IsValid will be false.MVC Example:
[HttpPost]
public IActionResult Save(Student student)
{
if (!ModelState.IsValid)
{
// Return same view with validation errors
return View(student);
}
// Save to DB
return RedirectToAction("Success");
}
Web API Example with [ApiController]:
[ApiController]
[Route("api/[controller]")]
public class StudentsController : ControllerBase
{
[HttpPost]
public IActionResult Create(Student student)
{
// No need to check ModelState.IsValid manually
// Invalid models return 400 automatically
return Ok(student);
}
}
[ApiController] for APIs to get automatic 400 responses.IValidatableObject or custom attributes for complex rules.ModelState.IsValid in MVC before processing.
ModelState.ClearValidationState(nameof(Model));
TryValidateModel(Model, nameof(Model));
Could be GET, POST, PUT, etc.
Data sources: Route values, query string, form fields, headers, JSON body.
ASP.NET Core inspects the request and matches incoming data to action parameters or model properties.
Sources are checked in a specific order: route → query → form → body → others.
If binding fails for a property, ModelState gets an error.
Runs immediately after binding.
Uses Data Annotations ([Required], [Range], etc.), IValidatableObject, or custom validators.
Validation results are stored in ModelState.
ModelState.IsValid and return the view with errors.Your business logic runs using the validated model.
Response is sent back to the client.
| Feature | Purpose | Triggered When | Common Use |
|---|---|---|---|
| Model Binding | Maps HTTP request data to parameters/models | Before action executes | Populate objects from form/query/JSON |
| Model Validation | Ensures data meets rules | After model binding | Enforce business/data constraints |
| Synchronous and Asynchronous in .NET Core | Securing ASP.NET Core Web API | |