Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The Hierarchy of Controller Class in ASP.NET MVC Architecture

0.00/5 (No votes)
23 Jul 2014 2  
In this article, we will understand the hierarchy of Controller class in ASP.NET, MVC framework

Introduction

There is no need to discuss how popular MVC architecture is in Microsoft’s web development platform. We all know MVC stands for model, view and controller where controller is like glue between model and view. It holds the business logic, formulates data and decisions which view to call and which data to be attached to it.

Fine, we know its functionality and we became happy to derive our controller class from “Controller” class and get our work done. We never thought why the controller class is and how it derived? I too was less bothered at first during my MVC days, but started to explore more about controller and found the big hierarchy of controller class. In this article, we will discuss hierarchy of controller class and we will see how the class had derived from various base classes and implemented various interfaces. At first, we will discuss the hierarchy of controller class and then we will see how we define our own controller class from “Control” class in MVC framework. So, let’s start our discussion.

IController is in Top Level

We can say that this is the top basest interface of controller class. The interface is very simple and clean, contains only one signature. Here is the IController interface.

namespace System.Web.Mvc
{
    // Summary:
    //     Defines the methods that are required for a controller.
    public interface IController
    {
        // Summary:
        //     Executes the specified request context.
        //
        // Parameters:
        //   requestContext:
        //     The request context.
        void Execute(RequestContext requestContext);
    }
}

It contains single signatures which need to implement in its concrete implementation. The controller is located within System.Web.Mvc namespace which is the primary DLL of MVC framework. The method takes request context as parameter.

ControllerBase Class which Implements IController

This is the next level of hierarchy where ControllerBase class implements IController interface. The ControllerBase contains six properties, one parameter less constructor and three methods. The top most popular and well known properties ViewData and ViewBag are defined within this class by which we can send data from controller to view. We can check that ViewBag is dynamic in nature which was introduced with MVC 3 and Viewdata is Dictionary in nature, we know that we need to store data in key value pair within ViewData. Actually ViewBag is nothing but an abstraction over ViewData and internally they use the same mechanism to store data. ViewBag gives flavor of dynamic characteristic. The rest of the functions and properties are described in comments. You can get the same view as you explore each class in Visual Studio. Point to remember is that, it’s an abstract class and we cannot create an instance of this.

// Summary:
    //     Represents the base class for all MVC controllers.
    public abstract class ControllerBase : IController
    {
        // Summary:
        //     Initializes a new instance of the System.Web.Mvc.ControllerBase class.
        protected ControllerBase();

        // Summary:
        //     Gets or sets the controller context.
        //
        // Returns:
        //     The controller context.
        public ControllerContext ControllerContext { get; set; }
        //
        // Summary:
        //     Gets or sets the dictionary for temporary data.
        //
        // Returns:
        //     The dictionary for temporary data.
        public TempDataDictionary TempData { get; set; }
        //
        // Summary:
        //     Gets or sets a value that indicates whether request validation is enabled
        //     for this request.
        //
        // Returns:
        //     true if request validation is enabled for this request; otherwise, false.
        //     The default is true.
        public bool ValidateRequest { get; set; }
        //
        // Summary:
        //     Gets or sets the value provider for the controller.
        //
        // Returns:
        //     The value provider for the controller.
        public IValueProvider ValueProvider { get; set; }
        //
        // Summary:
        //     Gets the dynamic view data dictionary.
        //
        // Returns:
        //     The dynamic view data dictionary.
        [Dynamic]
        public dynamic ViewBag { get; }
        //
        // Summary:
        //     Gets or sets the dictionary for view data.
        //
        // Returns:
        //     The dictionary for the view data.
        public ViewDataDictionary ViewData { get; set; }

        // Summary:
        //     Executes the specified request context.
        //
        // Parameters:
        //   requestContext:
        //     The request context.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     The requestContext parameter is null.
        protected virtual void Execute(RequestContext requestContext);
        //
        // Summary:
        //     Executes the request.
        protected abstract void ExecuteCore();
        //
        // Summary:
        //     Initializes the specified request context.
        //
        // Parameters:
        //   requestContext:
        //     The request context.
        protected virtual void Initialize(RequestContext requestContext);
    }

Controller Implements IController and ControllerBase and Many More

Yes, we will see that Controller class implements IController and Derived from ControllerBase and many more. Here is the implementation information. The Controller class is too abstract class so we cannot create an instance of it. Actually the Controller class contains lot of methods and properties. In this article, we are not interested in talking about them; we will just understand the hierarchy of Controller class in ASP.NET MVC.

public abstract class Controller: ControllerBase, IActionFilter, 
IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, 
IResultFilter, IAsyncController, IController, IAsyncManagerContainer

So, we are seeing that the Controller class is implementing many other interfaces which contain various useful functions to work out controller properly. For example, the IDisposable interface contains a method related to dispose the controller object after creation and various filter related interfaces are implemented to implement filter in controller level.

At Last, Define Our Own Controller Class

This is the last phase of hierarchy. Here, we can define our own controller which will be derived from the Controller class. The beauty of the hierarchy is, we are getting the readymade product and tool to start action immediately. As we are deriving our own Controller class from Controller class in Library, we are capable of using all the functions and properties which were already implemented in Controller class and BaseController class. Here is the way to derive our own controller class from Controller class.

MyController : Controller
{
}

Now, the question may come in mind, Why not defined our own controller class from BaseController or why not implement all those interfaces in our own controller class manually? Technically speaking, it’s possible to define control class in same approach, let’s have a look. In this example, we have derived our controller class from ControllerBase and try to run the application. Before we execute the controller, the ExecuteCore() will get executed and as we did not implement it’s implementation, it will throw an exception but always we can do it because we know MVC is open source application.

After ExecuteCore() is done, it will execute Hello() action. So the advantage is, Microsoft has already defined that stuff for us and we just need to use it.

Point to Remember

If you are new to MVC framework, then you may think that, why our Controller class ends with “Controller” suffix? The suffix is mandatory otherwise it will not be active. I wish to discuss the fact in another article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here