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.
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();
- }
- }
- }
Here you can see that controller factory implements three methods wherein, the first one is for controller creation, the third one is for session maintenance, and the third one is obviously for releasing the controller.
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web.SessionState;
- using MVC_Life_Cycle.Controllers;
-
- namespace MVC_Life_Cycle.CustomFactory
- {
- public class CustomFactory :IControllerFactory
- {
- public IController CreateController(RequestContext requestContext, string controllerName)
- {
-
- if (controllerName == "Sample")
- {
- return new SampleController();
- }
- else
- {
-
- return new HomeController();
- }
- }
-
-
- public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- {
- return SessionStateBehavior.Default;
- }
-
- public void ReleaseController(IController controller)
- {
-
- }
- }
- }
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.