Stage 3 - Controllers, Action Results, Model Binding, and Filters
Q21. What are controllers in ASP.NET Core?
Quick interview answer:
Controllers are classes that group related API endpoints or MVC actions. In Web API, controllers usually inherit from ControllerBase.
Study in detail: Controllers and Actions - This article explains controller structure and routes.
Q22. What is an action method?
Quick interview answer: An action is a public method inside a controller that handles one HTTP request and returns a response.
[HttpGet("{id}")]
public IActionResult GetStudent(int id)
{
return Ok();
}
Study in detail: Controllers and Actions - This article explains action routing.
Q23. What is [ApiController]?
Quick interview answer:
[ApiController] enables API-specific behavior such as automatic validation responses, improved parameter binding, and better API conventions.
Study in detail: Controllers and Actions - This article explains common controller attributes.
Q24. What is IActionResult?
Quick interview answer:
IActionResult is a flexible return type that allows an action to return different HTTP responses such as Ok, BadRequest, NotFound, or CreatedAtAction.
Study in detail: Action Results - Returning Correct HTTP Responses - This article explains status codes and result methods.
Q25. What is ActionResult<T>?
Quick interview answer:
ActionResult<T> tells readers and tools the expected success response type while still allowing different error responses.
public ActionResult<StudentDto> GetStudent(int id)
Study in detail: Action Results - Returning Correct HTTP Responses - This article compares IActionResult and ActionResult<T>.
Q26. When do you return Ok, CreatedAtAction, BadRequest, and NotFound?
Quick interview answer:
Return Ok for successful data, CreatedAtAction after creating a resource, BadRequest for invalid input, and NotFound when the requested record does not exist.
Study in detail: Action Results - Returning Correct HTTP Responses - This article gives practical API response examples.
Q27. What is model binding?
Quick interview answer: Model binding maps HTTP request data from route values, query strings, headers, forms, or JSON body into C# parameters and objects.
Study in detail: Model Binding and Validation - This article explains binding sources.
Q28. What are [FromRoute], [FromQuery], and [FromBody]?
Quick interview answer:
[FromRoute] reads from the URL route, [FromQuery] reads from query string, and [FromBody] reads from JSON request body.
public IActionResult Search([FromQuery] int classNumber)
Study in detail: Model Binding and Validation - This article explains source attributes.
Q29. What is validation in ASP.NET Core?
Quick interview answer:
Validation checks whether request data follows required rules before processing. Data annotations such as [Required], [Range], and [EmailAddress] are commonly used.
Study in detail: Model Binding and Validation - This article explains DTO validation.
Q30. What is ModelState?
Quick interview answer:
ModelState contains model binding and validation errors. Without [ApiController], developers often check ModelState.IsValid manually.
Study in detail: Model Binding and Validation - This article explains automatic and manual validation.
Q31. What are filters in ASP.NET Core?
Quick interview answer: Filters run code before or after controller actions. They are used for cross-cutting concerns such as action logging, authorization checks, result changes, or exception handling.
Study in detail: Filters - Running Code Before and After Controller Actions - This article explains filter types.
Q32. What is the difference between middleware and filters?
Quick interview answer: Middleware runs in the global HTTP pipeline. Filters run inside the MVC/controller pipeline and have action/controller context.
Study in detail: Filters - Running Code Before and After Controller Actions - This article compares middleware and filters.