Introduction
While programming with ASP.Net MVC, we should understand how ASP.NET MVC processes our requests and how many main stages there are in this process. There are primarily seven stages in the ASP.Net Request Life Cycle.
Routing
Routing is the first stage of the MVC Request Life Cycle. All requests to an ASP.NET MVC based application first pass through an UrlRoutingModule object (that is nothing but a HTTP module). The RouteCollection property of UrlRoutingModule contains all routes registered in the Global.asax file. UrlRoutingModule searches and matches for the route that has a URL. When a match is found, it retrieves the IRouteHandler object for that route. The route handler helps to get an IHttpHandler object and that is a HTTP handler for the current request. Route objects are added to the RouteTable object.
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- RegisterRoutes(RouteTable.Routes);
- }
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
- );
- }
MVC Request Handler (MVCRouteHandler)
MVC handler is responsible for initiating MVC applications. The MvcRouteHandler object creates an instance of the MvcHandler and passes the instance of RequestContext to MvcHandler. MvcHandler is implemented from ITttpHandler and it cannot map as a handler. This class does not support a parameterless constructor. It takes RequestContext as a parameter.
Controller
MVCHandler receives a controller instance from the MVC controller factory (IControllerFactory). This controller handles further processing of the request. The MVCHandler object uses RequestContext (passed as a constructor parameter) instance to identify the IControllerFactory object to create the controller instance. The MvcHandler instance calls the Execute method of controller.
Create and register Custom Controller Factory
- public class CustomControllerFactory : IControllerFactory
- {
- public IController CreateController(RequestContext requestContext, string controllerName)
- {
- //TODO: IoC implementation or Creating controller from request context.
- return null;
- }
- public void ReleaseController(IController controller)
- {
- var disposable = controller as IDisposable;
- if (disposable != null)
- {
- disposable.Dispose();
- }
- }
- public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- {
- throw new NotImplementedException();
- }
- }
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
- RegisterRoutes(RouteTable.Routes);
- }
- }

Sureshkumar BosePosted Dec 8, 2019, 7:00 AM
Thanks for your explanation
Revanth SaiPosted Apr 5, 2019, 4:01 AM
Thanks a lot ..! you cleared all my doubts.
Amir NavedPosted Mar 28, 2019, 12:38 AM
Really Helpfull
Atul SharmaPosted Mar 12, 2019, 9:50 AM
Very well written and summarized article !!