MVC Life Cycle – 4th Part

Hi Friends,

In this section, we are going to see how Controllers are created and initialized.Basically, controllers does nothing but implement IController interface. IController defines one important method called Execute(). Also, it is important to note that this method is entirely generic which means you can build your controller in a more custom way specific to particular requirement. However, MVC is very powerful framework wherein all the concerns are already addressed. But, you can extend the default implementation for any specific need.

Here, ControllerFactory here comes into picture. Here, it is used by MVCHandler. Now, ControllerFactory’s primary responsibility is to provide appropriate type of controller to service the request. Again, ControllerFactory is tied with IControllerFactory. Hence, without wasting time, let us go ahead and jump to the demo. In the below snippet, I have created one sample controller and implemented IController here.

Above snippet is the raw snippet obviously.Now, let us go ahead and modify the same.

Now, let us go ahead and create one controller factory which will basically implement our custom controller. Now, you may be thinking there is lots of stuff going here, which is true by the way in this context.

Now, here you can see that controller factory implements three methods wherein, 1st one is for controller creation, 2nd one is for session maintenance and 3rd one is obviously for releasing the controller.

Now, in order to register the same, I need to make the changed in global.asax file itself.

With the above change in place, when I run the app and navigate to URL http://localhost:44694/Sample, then it will produce the following output.

17th 18th

and then it will produce the default result.


I hope you would have liked this discussion. Will continue to delve further in coming session. Till then, stay tuned and Happy Coding.

Code Download Link:-

Rahul Sahay
Happy Coding