Home > ASP.NET MVC > ASP.NET # MVC # 14 – How Asp.net MVC controller works (Internally in dot net framework) or how controller class is implemented??

ASP.NET # MVC # 14 – How Asp.net MVC controller works (Internally in dot net framework) or how controller class is implemented??

Hi Geeks,

As we have seen in the post Controllers in detail that –

Controller is the most important part in MVC architecture which works as a mediator in between views and models.

I have  made one simple class diagram which will explain how normal class is converted into the controller or how controller works implicitly in dot net framework.

 

Controller

I

For a class to be a controller ,at least it should implement the Controller interface as shown in the diagram in 1) red box and by convention the name of the class must end with the suffix Controller.

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

EX. Simple Example for implementation of IController interface

using System.Web.Mvc;
using System.Web.Routing;
public class SimpleController : IController
{
           public void Execute(RequestContext requestContext)
          {
            var response = requestContext.HttpContext.Response;
            response.Write(“<h1>Hello World!</h1>”);
           }
}

It’s a simple process really: When a request comes in, the Routing system identifies a Controller, and it calls the Execute method

When we put the name of the controller in the url like http:localhost//Simple ,We will have output as follows

image

NOTE : Implementing IController is pretty easy, as you’ve seen, but really all it’s doing is providing a facility for Routing to find your Controller and call Execute. This is the most basic hook into the system that you could ask for, but overall it provides little value to the Controller you’re writing.

II

AS we are developers we always love to work with rich API(application programming interface).As we can see in the 2) red box a class can also inherit Controller class to be a controller .

This Controller class is still very lightweight and allows developers to provide extremely customized implementations for their own Controllers, while benefiting from the action filter infrastructure in ASP.NET MVC

Controller class implements ControllerBase class which implements IController.

ControllerBase class is an abstract base class that layers a bit more API surface on top of the IController interface. It provides the TempData and ViewData properties.

Controller class also implements interfaces  like  IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter

These interfaces are useful in many other functionality like Exception Handling , Authorization .

NOTE: As we saw two ways to use class as a controller but The standard approach to writing a Controller is to have it inherit from the System.Web .Mvc.Controller abstract base class, which implements the ControllerBase base class. The Controller class is intended to serve as the base class for all Controllers, as it provides a lot of nice behaviours to Controllers that derive from it.

I hope this will remove all the queries related to controller from your mind.

Thank You.

Advertisements
Categories: ASP.NET MVC
  1. No comments yet.
  1. No trackbacks yet.

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 )

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

%d bloggers like this: