Whenever the request comes from the users, it is processed in the following sequence in order to render the view to the user.
As we can see from the above diagram whenever a request comes from the user, first it goes to the routing system then it is transferred to the ControllerFactory which internally decides which controller to call and then in turn which action to be invoked is decided by the controller, so that the requested view is returned to the user. In this case every request coming from the user has to go through the ControllerFactory. Now you must be wondering we’d not created any ContollerFactory in our application well that rights ASP.Net MVC provides you with the default ContorllerFactory which does the work of finding the appropriate controller and processing your request. The
"DefaultControllerFactory" is the class provided by mvc as a ControllerFactory. It basically implements the interface
"IControllerFactory" which has certain methods which does the loading and creation of controller. Following are the methods defined inside "
IControllerFactory" interface.
- IController CreateController(RequestContext requestContext, string controllerName)
- SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- void ReleaseController(IController controller)
Well Microsoft has provided you with the benefit of flexibility so that you can change the default behavior of working with mvc to your own customizable way. Here the example just demonstrate that you can change the default working of ControllerFactory to the way you defined, by just creating a class which implements the IContollerFactory interface. But this is not the ideal way for doing things that because if you try to implement your own ControllerFactory you need to make sure about the various issues that may occur during execution and working of your controller. Issues like
- Properly handling user request and loading the right Controller which is required
- Loading the controller dynamically
- Resolving the ambiguity issues arising because of same class name within different namespaces.
- Handling exception which may occur during runtime
Note
- This is just a demonstration to known/ understand that we can incorporate our own implementation logic in creation of controllers. By creating a class which in turn implements the IControllerFactory interface.
- If you take my view, then the built in "DefaultControllerFactory" is best one to use for some scenarios where the things to be done are not that complex.
- Well some developer makes use of customized controller factory for doing dependency injection via constructors or for writing their own way for processing a request.
Ok now let’s start with the creation of the "CustomizeControllerFactory class". For this I’ve just added a new folder with the name “Infrastructure” in the application.
Here is the code snippet for the same.
- public class CustomControllerFactory : IControllerFactory
- {
-
-
-
-
-
-
- public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
- {
- Type controllerType = Type.GetType(string.Format("ControllersInDepth.Controllers.{0}", controllerName));
-
- IController tempController = Activator.CreateInstance(controllerType) as IController;
- return tempController;
- }
-
-
-
-
-
-
- public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
- {
- return System.Web.SessionState.SessionStateBehavior.Default;
- }
-
-
-
-
- public void ReleaseController(IController controller)
- {
- IDisposable dispoable = controller as IDisposable;
- if (dispoable != null)
- dispoable.Dispose();
- }
}
Once we are done with the creation of CustomControllerFactory we need to register it so that it be put to work. To register we need to add an entry inside the "Global.asax" file in Application_Start function. Here is the code snippet for the same.
-
- ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
Now we can start testing the application. For demonstrating this we’ve added a new model to the application with the name "ControllerDetails.cs" Here is the code snippet for the same.
- namespace ControllersInDepth.Models
- {
- public class ControllerDetails
- {
- public string ControllerName { get; set; }
- }
- }
Also we’ve created a new view in the shared folder with the name "Test.cshtml" and also added two controllers with the name “Home” and "Employee" on each index action method we are calling binding the Model "ControllerDetails" and calling the view "Test.cshtml".
Also in the RouteConfig.cs file we’ve added a new entry for the routing. Here is the code snippet.
- routes.MapRoute(
- name: "customized",
- url:"{controller}/{action}"
- );
You can now start using the application. By simply running it. Here is the snapshot for the same.
Now just type /Employee/ in the address bar and hit enter. You’ll find that the controller name is now changed.