What is Controller in ASP .NET MVC?

Controller in ASP .NET MVC:

The Controller in MVC architecture handles any incoming URL request. The Controller is a class, derived from the base class.

Controller class:

Despite the explicit reference to the Model-View-Controller pattern in the name, the ASP.NET MVC architecture is essentially centered on one pillar—the controller. The controller governs the processing of a request and orchestrates the back end of the system to grab raw data for the response. Next, the controller wraps up raw data computed for the request into a valid response for the caller. When the response is a markup view, the controller relies on the view engine module to combine data and view templates and produce HTML.

Aspects of a controller

Any request that passes the URL routing filter is mapped to a controller class and served by executing a given method on the class. Therefore, the controller class is the place where developers write the actual code required to serve a request.

The inherent statelessness of the controller and its neat separation from the view makes the controller class potentially easy to test. However, the real testability of the controller class should be measured against its effective layering.

What is Controller in ASP .NET MVC

Writing controller classes:

The writing of a controller class can be summarized in two simple steps: creating a class that inherits (either directly or indirectly) from Controller, and adding a bunch of public methods. However, a couple of important details must be clarified: how the system gets to know the controller class to instantiate, and how it figures out the method to invoke.

From routing to actions:

When the ASP.NET MVC run-time environment has a valid instance of the selected controller class, it yields the action invoker component for the actual execution of the request. The action invoker gets the action name and attempts to match it to a public method on the controller class.

The action parameter indicates the name of the action to perform. Most of the time, the controller class just has a method with the same name. If this is the case, the invoker will execute it.

Example:

public class HomeController: Controller
{
// Implicit action name: Index
public ActionResult Index()
{
...
}
[NonAction]
public ActionResult About()
{
...
}
[ActionName("About")]
public ActionResult LoveGermanShepherds()
{
...
}
}

Actions and HTTP verbs:

ASP.NET MVC is flexible enough to let you bind a method to action for a specific HTTP verb. To associate a controller method with an HTTP verb, you either use the parametric AcceptVerbs attribute or direct attributes such as HttpGet, HttpPost, and HttpPut. Using the AcceptVerbs attribute, you can specify which HTTP verb is required to execute a given method.

Example:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Customer customer)
{
...
}

Action methods:

An action method grabs available input data by using any standard HTTP channels. Next, it arranges for some action and possibly involves the middle tier of the application.

Example:

public class HomeController: Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}