Controllers In MVC
Introduction To Controllers
The controller, as the name implies, controls the application logic and acts as the coordinator between the view and the model.
Controllers receive input from users via the view, then work with the model to perform specific actions, passing the results back to the view.
Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.
All controller classes must be named by using the "Controller" suffix.
User requests an MVC application through the browser, controller is responsible for returing the response to that request. Controller undergoing one or more actions and returns action results to browser.
The actions may return files,views or may redirect to another controller actions.
There will be a controller folder which contains controller classes.
These are responsible for processing incoming requests, handling input, saving data, and sending a response to send back to the client.
Home Controller.cs contains some actions include Index and About.
The example given below
Adding Controller To Application
To add a new controller to the application,
The following is showing a sample controller class named as StudentController
A controller exposes controller actions.
An action is a method on a controller that gets called when you enter a particular URL in your browser address bar.
The methods in controllerclasses referred to as controller actions that do all the heavy lifting that's involved in processing requests.
Controller With Multiple Actions
The following is showing a sample controller class named as StudentController with multiple actions.