In ASP.Net MVC, The ‘M’ stands for Model, The ‘V’ stands for View and ‘C’ stands for Controller. The Controller is the most important part in the MVC triad. Let us discuss more about Controllers, Controller actions and Action results in this post.
So , What is a Controller?
In the most simplest terms, a controller is a module that is responsible for responding to a request (generally a browser request). Every request that is made to an ASP.Net MVC application is mapped over (routed) to a particular controller ( in other words, every controller is designed to handle\ serve a specific kind of request). Hence a controller is actually something that controls our interaction with an ASP.Net MVC application or we can say that a controller is responsible for controlling the interaction of an ASP.Net MVC application.
The controller serves every request that it receives by performing an action. These actions are generally called as Controller actions. Every action that the controller performs generates a result. This result is then send back to the entity that initiated the request (mostly a browser). These generated results are called as Action Results.
So, Let us now understand what Controllers, Controller actions and Action Results are in ASP.Net MVC framework
So , A controller is just a class that takes a request and then returns some response to the request. Every controller derives from a base Controller class which belongs to System.Web.MVCController.
Every controller class should end with the suffix ‘Controller’. Eg: ProductController, SampleController etc..
Every controller is made up of actions. A controller action is the actual method that gets invoked when we make a request against the controller. The particular action that is to be called depends on what we type in the url , in other words, what functionality has to be performed by the controller is determined by the kind or type of url it gets.
However, there are few requirements that a controller action has to satisfy –
- An action is a method that is exposed by the controller. Hence, a controller action has to be a public function.
- Every controller performs a unique action i.e no two actions are ambiguous. Hence a controller can have only one version of action which means that a controller function cannot be overloaded (to have multiple versions of the same function).
- A controller action cannot be shared i.e every instance of the controller will have its own action instance which means that actions should always be instance methods..
- A controller action can have any name i.e we can use any function that we want as a controller action.
(to be continued …)