Controllers in ASP.Net MVC.

Reference : Understanding Controllers, Controller Action and ActionResult and ASP.Net MVC Controller Overview.

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..

Controller Actions
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 –

  1. An action is a method that is exposed by the controller. Hence, a controller action has to be a public function.
  2. 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).
  3. 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..
  4. A controller action can have any name i.e we can use any function that we want as a controller action.
Every action performed by a controller generates a result. This is called as ActionResult (i.e result of an action).  This result is returned back as the response or in other words every request that is made to the controller recives  an ActionResult as a response.
Every action can produce different kinds of result, hence, depending on this there are number of different types of ActionResult that a controller action might return – viz..
  1. ViewResult.
  2. RedirectResult.
  3. JSONResult.
  4. JavaScriptResult.
  5. ContentResult.
  6. FileContentResult.
  7. FilePathResult.
  8. FileStreamResult.
  9. EmptyResult.
(Do you smell an implementation of State Pattern in case of ActionResult and also Null Pattern in case of EmptyResult.)
The most common type of ActionResult that is returned is a View or a ViewResult.  So, what is a ViewResult? A ViewResult is an ActionResult that returns a View. A View contains markups, Html markups and some content that you want to send back to the browser.
Similiarly, there are different types of data that the action can return.
Typically, we do not create these results directly. What we do instead is that we call one of the methods of the base controller class (remember – our controller class inherits from System.Web.MVCController), to return the different types of results. The different types of Controller class methods available are :
  1. View.
  2. Redirect.
  3. RedirectToAction.
  4. RedirectToRoute.
  5. Json.
  6. JavaScript.
  7. Content.
  8. File.
So, if we want to return a ViewResult, we do not create a View, instead,  we return a View by calling the View() method of the controller base class.
Let us see how this works :
When  a View() method is called on the base controller class, it returns a ViewResult. Now, there is a specific pattern that is followed by the View method to find the ViewResult that is to be returned.
If no name of the ViewResult is specified then the View() method returns the  ViewResult that corresponds to the name of the Controller action i.e if the controller action from where View() is called is named Specification, then the View() method will try to return a ViewResult named “SpecificationView”, which should be located in the Views subfolder that corresponds to the name of the controller.
Eg :
if we have the following action in Controller named Product :
public ActionResult Specification()
    return View();
Then the ViewResult returned will be SpecificationView which should be located in Views>Product>Specification.aspx.
To do this we will add a subfolder in View folder having name of the Controller (i.e Product) and then add a MVC View page which should have the name of the action i.e Specification, in this case.

(to be continued …)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s