[ASP.NET MVC] Controllers – Overview

A controller is responsible for how a user interacts with an MVC application. A controller interprets a user’s request and determines what response to send back to the user.

A controller is just a class, which inherits from the “System.Web.Mvc.Controller” class.

1. IController

In MVC, controllers must implement the “System.Web.Mvc.IController” interface, whose sole purpose is to execute some code when a request is made to a controller.

public interface IController
{
  void Execute(RequestContext requestContext);
}

The “System.Web.Routing.RequestContext” class encapsulates information about an HTTP request that matches a defined route. It has two properties:

  • public virtual HttpContextBase HttpContext { get; set; }
  • public virtual RouteData RouteData { get; set; }

Through the “HttpContext” object, you can access all necessary data regarding the request: Cache, Profile, Request, Response, Session, etc …

public class SimpleController : IController
{
  public void Execute(RequestContext requestContext)
  {
    string controller = requestContext.RouteData.Values["controller"] as string;
    string action = requestContext.RouteData.Values["action"] as string;
    string id = requestContext.RouteData.Values["id"] as string;

    requestContext.HttpContext.Response.Write(string.Format("{0} - {1} - {2}", controller, action, id));
  }
}

Run the application and navigate to “Simple/Hello/1” and you can see the “Simple – Hello – 1” on the screen.

 

2. ControllerBase

The abstract “System.Web.Mvc.ControllerBase” class represents the base class for all MVC controllers. It implements the “IController” interface and provides the following properties:

  • public ControllerContext ControllerContext { get; set; }
  • public Object ViewBag { get; }
  • public ViewDataDictionary ViewData { get; set; }
  • public TempDataDictionary TempData { get; set; }

 

3. Controller

The “System.Web.Mvc.Controller” class represent the MVC controller. If you create a controller in an MVC project, the controller class will derive from the “Controller” class.

  • public HttpContextBase HttpContext { get; }
  • public ModelStateDictionary ModelState { get; }
  • public HttpRequestBase Request { get; }
  • public HttpResponseBase Response { get; }
  • public RouteData RouteData { get; }
  • public HttpServerUtilityBase Server { get; }
  • public HttpSessionStateBase Session { get; }
  • public UrlHelper Url { get; set; }

The “Controller” class implements the “IController.Execute()” method and takes responsibility for invoking the action method whose name matches the action value in the route data.

 

4. How to Create a Controller

By convention, controller classes located in the “Controllers” folder and their names are suffixed with “Controller“.

You can add a controller by right-clicking the “Controllers” folder and selecting “Add -> Controller” menu. This will show you the “Add Controller” dialog. At this point, you have the option to generate Create, Update, and Details action methods automatically by checking the option.

The controller is just a class. So you can create a class directly in the “Controllers” folder. Just make sure the class name ends with a “Controller” suffix, and the class inherits from “System.Web.Mvc.Controller“.

 

5. Three Big Features of a Controller

Controllers provides the following key features:

  • Action Methods: represent the controller’s behavior
  • Action Results: represent the results of action methods
  • Filters: encapsulate the resuable behavior for action methods and can be defined declaratively by adding the atrributes to an action method

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s