Controllers

Broadly speaking, a controller is a class with a group of methods called actions. An action, also known as an action method, is a method that handles the request. As per the convention used by the ASP.NET Core MVC framework, a controller is a class that:

  • Is instantiable; that is, an instance of that class can be created
  • Resides in the project's root-level folder, named Controllers
  • Inherits from an abstract class, Microsoft.AspNetCore.Mvc.Controller

The framework is flexible and would also treat an instantiable class as a controller if any one or more of the following holds good:

  • The class is decorated with the [Controller] attribute
  • The class name is suffixed with Controller, such as HomeController
  • The class derives from a class, whose name ends with Controller

An important thing to note here is that we have a controller defined, even if we do not derive our controller from the Controller class. As discussed earlier, the role of a controller is to validate the request data and return the result in the form of a View or data. In the ASP.NET Core MVC project structure, we can see a Controllers folder at the project root level, as shown here:

Let's have a look at the code map diagram of the Controller class to understand it better:

The diagram reveals that the Controller class itself derives from the ControllerBase class, by which it gets access to HttpContext and ControllerContext properties. It has properties for TempData, ViewData, and ViewBag, which we will discuss when we discuss the Views in the next chapter. The class contains a bunch of methods, mostly for returning action results and executing action filters. Let's have a look at them:

  • PartialView: Creates a Microsoft.AspNetCore.Mvc.PartialViewResult object that renders a partial View to the response
  • View: Creates a Microsoft.AspNetCore.Mvc.ViewResult object that renders a View to the response
  • Json: Creates a Microsoft.AspNetCore.Mvc.JsonResult object that serializes the data to JSON
  • ViewComponent: Creates a Microsoft.AspNetCore.Mvc.ViewComponentResult by specifying the name of a View component to render
  • OnActionExecuting: Called before the action method is invoked
  • OnActionExecuted: Called after the action method is invoked
  • OnActionExecutionAsync: Called before the action method is invoked in an async implementation

Not all methods defined in the Controller class are actions. Only public methods which are not decorated with the [NonAction] attribute are actions. An action method can return anything, but generally, we will see them return an instance of IActionResult (or Task<IActionResult> for async methods). An action method decides the kind of response it would return; it may be a View or formatted response data, such as JSON, or anything else.

With the intent of separation of concerns and loose coupling, the Controller class should always have the dependencies injected to it, rather than creating the instance of it. If a type or service is required for only one action method, we can use the injection of a service directly in the action by using the [FromServices] attribute that we saw earlier; otherwise, the construction injection should be used.

We will end our lap around MVC with an overview of error handling.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.232