Before reading this article, I highly recommend you read my previous parts:
This is the continuation of the MVC Life Cycle Series. If you are referring this article as a standalone, I would encourage you to go through the entire series in order to understand the entire gist of the same.In this section, we are going to see how Controllers are created and initialized. Basically, controllers do 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 following snippet, I have created one sample controller and implemented IController here.
- using System;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace MVC_Life_Cycle.Controllers
- {
- public class SampleController : IController
- {
- public void Execute(RequestContext requestContext)
- {
- throw new NotImplementedException();
- }
- }
- }
Above snippet is the raw snippet obviously. Now, let us go ahead and modify the same.
- using System;
- using System.Net.Http;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace MVC_Life_Cycle.Controllers
- {
- public class SampleController : IController
- {
- public void Execute(RequestContext requestContext)
- {
- //Displaying some message
- HttpContext.Current.Response.Write("Hello From Custom Controller Implementation");
- }
- }
- }
Now, let us go ahead and create one controller factory which will basically implement our custom controller. Now, you may be thinking there are lots of stuff going here, which is true by the way in this context.
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web.SessionState;
- namespace MVC_Life_Cycle.CustomFactory
- {
- public class CustomFactory :IControllerFactory
- {
- public IController CreateController(RequestContext requestContext, string controllerName)
- {
- throw new System.NotImplementedException();
- }
- public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- {
- throw new System.NotImplementedException();
- }
- public void ReleaseController(IController controller)
- {
- throw new System.NotImplementedException();
- }
- }
- }




Manoj KallaPosted Jan 30, 2016, 11:40 AM
Good one
Pradeep SahooPosted Jan 24, 2016, 10:53 AM
Thanks Rahul for such internals , can you please tell me some scenario where Execute() need to be written some custom logic .
Santhakumar MunuswamyPosted Jan 23, 2016, 2:05 AM
Thanks for nice share
Ankit BansalPosted Jan 22, 2016, 12:54 AM
keep sharing..thanks
Humayun Kabir MamunPosted Jan 22, 2016, 12:07 AM
Nice...