Controllers in ASP.NET Core MVC
A Controller is a special class in ASP.NET Core Application with .cs (for C# language) extension. The controller class should and must be inherited from the Controller base class.
Controllers in MVC Design Pattern are the components that handle user interaction, work with the model, and ultimately select a view to render. The controller handles and responds to user input and interaction
In the MVC Design Pattern, the controller is the initial entry point and is responsible for selecting which model types to work with and which view to render
How Request Pipeline works in an ASP.NET Core MVC Application
When the client/browser sends a request to the server, that request first goes through the 'request processing pipeline'.
Once the request passes, the 'request processing pipeline', it will hit the controller.
Inside the controller, there are lots of methods (called action methods) that actually handle the incoming HTTP Requests.
The action method inside the controller executes the business logic and prepares the response, which is sent back to the client who initially made the request.
Role of Controller in MVC:
A Controller is used to group actions, i.e., Action Methods.
The Controller is responsible for handling the incoming HTTP Request.
The Mapping of the HTTP Request to the Controller Action method is done using Routing. That is, for a given HTTP Request, which action method of which controller is going to be invoked is decided by the Routing Engine.
Many important features, such as Caching, Routing, Security, etc., can be applied to the controller.
Controller Tempaltes in ASP.NET Core
ASP.NET Core supports 3 type of controller tempates: -
MVC Controller – Empty: It will create an Empty Controller.
MVC Controller with read/write actions: This template will create the controller with five action methods to create, read, update, delete, and list entities.
MVC Controller with views, using Entity Framework: This template will create an MVC Controller with actions and Razor views to create, read, update, delete, and list entities using Entity Framework.
Controller base class
The controller base class is present in Microsoft.AspNetCore.Mvc namespace
The Controller is an abstract class having many methods (Json, View, PartialView, OnActionExecuting, etc.) and properties (TempData, ViewBag, ViewData, etc.),
What are Action Methods?
All the public methods of a controller class are known as Action Methods
The Controllers logically group similar types of actions together. This aggregation of actions or grouping of similar types of action together allows us to define sets of rules, such as caching, routing, and authorization, which will be applied collectively.
An action method can return several types.
How is Controller Instance Created in ASP.NET Core MVC?
In order to create ASP.NET Core MVC Application, we need to add the required MVC Services and Middleware into the Request Processing Pipeline.
Add the MVC services within your Main method of the Program.cs class file in .NET 6 Application.
Configure the MVC Middleware into the Request Processing Pipeline - either using conventional or attribute routing
In ASP.NET Core MVC Web Application, the MVC Middleware Component will receive the request when the client sends an HTTP Request.
Once the MVC Middleware receives the request, based either on the conventional or attribute routing, it will select the controller and action method to execute.
In order to execute the action method, the MVC Middleware must create an instance of the selected controller, because we can't access non-static method until we create an instance of a class.
In order to create an instance of the Controller class, the MVC Middleware uses the concept called reflection, and it will use the IControllerActivator class.
object Create(ControllerContext context): This method is used to Create a controller. Here, the parameter context is used for executing the action method.
void Release(ControllerContext context, object controller): This method is used to Release a controller. Here, the parameter context is used for executing the action method. And the parameter controller specifies the controller to release.
ValueTask ReleaseAsync(ControllerContext context, object controller): This is the same as the Release method, but it Releases a controller asynchronously.
The above interface is implemented by a class called DefaultControllerActivator, which will implement the IControllerActivator interface methods and provides the logic to create and release the controller.