Overview
A controller defines a set of actions. An action is a method on a controller which handles requests. Requests are mapped to actions through routing.
A controller is responsible for initial processing of the request and instantiation of the model. Business logic should remain in the model.
The controller:
- validates request data
- takes the result of the model’s processing (if any)
- returns either the appropriate proper view or the result of the API call.
The controller’s responsibilities do not include data access or business logic it instead delegates to services handling these concerns. Business concerns should be represented as services that the controller accesses through DI.
Controllers reside in Controller/
and inherit from Microsoft.AspNetCore.Mvc.Controller
.
Controller classes:
- are instantiable
- are usually public
- are suffixed with “Controller”
- have the
[Controller]
attribute
Approach
If multiple controller actions require the same service, use constructor injection to request the dependencies. If the service is needed by only a single controller action, use action injection to request the dependency.
Define a Controller
Public methods on a controller are actions. Parameters on actions are bound to request data and are validated using model binding.
Validation occurs for everything that’s model-bound. ModelState.IsValid
is boolean if binding and validating succeeded.
Actions usually return an IActionResult
that produces a response.
- The action method chooses the type of response.
- The
IActionResult
does the responding.
Controller Helper Methods
There are three types:
- Methods resulting in an empty response body
- HTTP Status Code result types:
BadRequest()
,NotFound()
,Ok()
- Redirect—includes a Location HTTP response header
Redirect()
,LocalRedirect()
,RedirectToAction()
,RedirectToRoute()
- HTTP Status Code result types:
- Methods resulting in a non-empty response body with predefined content
- View—returns a view which uses a model to render HTML
return View(customer);
- Formatted Response—returns JSON to represent an object
return Json(customer)
,File()
,PhysicalFile()
- View—returns a view which uses a model to render HTML
- Content Negotiation
- Applies whenever an action returns an
ObjectResult
type - An action that returns a non-
IActionResult
implementation (like object) also returns a Formatted Response BadRequest(modelState)
,CreatedAtRoute("routename", values, newobject)
,Ok(value)
- Applies whenever an action returns an