ASP.NET WEB API Flashcards
What happens if you remove [ApiController]?
Model validation errors won’t trigger 400 Bad Request automatically.
How do you return a custom HTTP response in an API controller?
return StatusCode(201, new { message = “Created” });
API Controller
An API Controller in ASP.NET Core is a specialized controller that handles HTTP requests and is optimized for RESTful API development. It derives from ControllerBase and typically uses the [ApiController] attribute for automatic request validation and behavior enhancements.
Marked with [ApiController] for automatic model validation, routing, and binding improvements.
API Controllers process HTTP requests and return structured data (usually JSON).
[ApiController] enforces attribute routing, which means that conventional routing is disabled.
How are API Controllers and MVC Controllers different
Unlike MVC controllers, they do not return HTML views.
Use Case
RESTful API (JSON, XML) vs Web apps (HTML, Razor Views)
Inheritance
ControllerBase vs Controller
Return Type
IActionResult, JSON vs ViewResult, PartialView
Behavior
Lightweight, optimized for APIs vs Heavy, supports views
Model Validation
Automatic via [ApiController] vs Manual validation required
How do you handle versioning in an API controller?
Use [ApiVersion] and configure API versioning in Program.cs.
Why use API Controllers
Why use API Controllers instead of MVC Controllers for APIs?
Lightweight: No view rendering overhead.
Built-in Model Validation: Reduces manual validation logic.
Automatic HTTP Response Handling: Converts exceptions to proper status codes.
Better API Design: Encourages proper RESTful practices.
Optimized for stateless RESTful APIs.
No unnecessary MVC overhead (views, ViewData, ViewBag, etc.).
Automatic model validation and serialization.
Explicit HTTP method mapping ([HttpGet], [HttpPost], etc.).
Better API response handling (returns JSON/XML by default).
How does [Route] help in API controllers?
Defines a route template for a controller or action, guiding request matching.
What is [NonAction] used for?
Prevents a public method from being treated as an API action.
What does [AllowAnonymous] do?
Allows unauthenticated users to access an action.
What happens if you apply [HttpGet] and [HttpPost] to the same action and send a DELETE request?
The request will return a 405 Method Not Allowed error because the action does not handle DELETE.
What is the default binding source for primitive types in controller action parameters?
Query string ([FromQuery]), unless overridden.
Can an action have multiple HTTP method attributes, like [HttpGet] and [HttpPost]?
What happens if [HttpGet] and [HttpPost] are applied on the same method with different parameters?
Yes, an action can support multiple HTTP methods by applying multiple attributes, e.g., [HttpGet, HttpPost]. However, it may cause ambiguity in some cases.
Model binding may fail, or 400 Bad Request may occur.
What is the default binding source for primitive types, complex types, method without http verb attribute?
Primitive : FromQuery
Complex :Request body ([FromBody]),
HTTP Verb : POSY
What happens if an API controller has [Route(“api/[controller]”)] and an action has [Route(“get-data”)]?
The final route will be api/{controller}/get-data, replacing [controller] with the actual controller name.
What happens if you apply [Route(“{id}”)] to a POST method?
The route may not work correctly because POST typically expects data in the body, not in the route.
What happens if you use [Route] on an action but not on the controller?
The action’s route is treated as an absolute route, not inheriting any prefix.
What happens if you apply [NonAction] to a private method inside an API controller?
Nothing changes because private methods are not treated as actions by default.
The method is ignored by routing.
Can [FromBody] be used with multiple parameters in a single action method?
Can you apply [FromBody] and [FromForm] on the same parameter?
No, only one parameter per action can be bound from the body. ASP.NET Core does not allow multiple [FromBody] parameters.
No, a parameter can be bound from only one source.
What happens if an API controller has both [Authorize] and an action has [AllowAnonymous]?
The action will be publicly accessible despite the [Authorize] on the controller.
Can Any Attribute Be Used for Performance?
Directly? No. Attributes in ASP.NET Core are metadata—they do not directly improve performance. However, some attributes indirectly impact performance by controlling request processing.
Attributes That Indirectly Affect Performance:
[Produces] & [Consumes] → Reduces unnecessary content negotiation.
[ApiController] → Auto model validation prevents extra logic execution.
[ResponseCache] → Enables response caching, reducing load.
[ProducesResponseType] → Helps with API documentation but does not improve performance.
[Authorize] & [AllowAnonymous] → Controls authentication overhead.
[NonAction] → Ensures non-API methods are not mistakenly invoked.
How Is Attribute Precedence Determined?
🔹 1. Controller-Level Attributes override global settings but are overridden by method-level attributes.
🔹 2. Method-Level Attributes have the highest precedence and take priority over controller-level attributes.
🔹 3. Global Filters (via AddControllers in Startup.cs) apply to all controllers unless overridden.
What happens if [ResponseCache(Duration = 60)] is used with [HttpPost]?
No effect, because POST responses are usually not cached.
Attribute Precedence Within the Same Method
Action Filter Attributes ([Authorize], [ValidateAntiForgeryToken], etc.)
Executed before the action method runs.
Applied in the order from class-level to method-level (method-level can override).
Model Binding & Validation ([FromBody], [FromQuery], [ApiController])
Happens before filters.
[ApiController] auto-validates models before the action executes.
Routing & HTTP Verbs ([HttpGet], [Route], etc.)
Defines how the request is matched before execution starts.
[Route] on a method overrides controller-level [Route].
Response Modifiers ([Produces], [ResponseCache], [ProducesResponseType])
Applied after the action executes, affecting the response.
What happens if [Produces(“application/json”)] and [Produces(“text/plain”)] are both applied?
The API only supports the last-specified content type.