The MVC Request Life Cycle
The entry point of MVC Request life cycle is URL Routing module, the incoming request from IIS pipeline is handed over to URL Routing module which analyses the request and looks up Routing table to figure out which controller the incoming request maps to.
"Routing Table is a static container of routes defined in MVC application with corresponding controller action mapping. If the route is found in the routing table MVCRouteHandler executes and brings the instance of MVCHttpHandler".
Together they act as a gateway into the MVC Framework. MVC handler begins initializing and executing controller. The MVCHttpHandler also takes of converting route data into concrete controller that is capable of serving the request. MVC handler does all this with the help of MVC Controller factory and activator which are responsible for creating an instance of the controller. This is also the place where the Dependency Injection is performed if the application has been designed to invoke parameterized controller constructor and satisfy its dependencies.
After the controller instance is created the next major step is to find and execute the corresponding action. A component called ActionInvoker finds and executes the action defined in routing table. Before the action method is called model bindings takes place which maps data from http request to action method parameters. After the model binding, action filters are invoked which includes OnActionExecuting filter. This is followed by action execution and Action Executed filter execution and finally preparing Action Result.
Once the Action method has been finished executing the next step is Result execution. MVC separates the action declaration from Result execution. If the Result from action execution is view, then depending upon configuration, ASPX or Razor view engine will be called to find and render the html view as a response of http request. If the result was not view then it’s passed as-is to http response.
Following is the conceptual view of MVC request life cycle.
Page Life cycle of Asp.Net MVC |
WebForms vs MVC
If you have ever developer a web forms application, then this is important scenario to understand about the difference between both.
- In Web forms life cycle, service corresponds to the physical file. However in MVC, this concept is completely different as Controller Actions are executed to render a view to the view.
- Inspite of having the differences, both Web forms and MVC requests are implemented through HttpHandler. In web forms each page is derived from IHttpHandler interface and request is served more directly. MVC controllers are also derived from HttpHandlers. From a higher level, MVC is just another way to manage request from ASP.net platform.
- Route Table Execution When application is run first time Application_Start method is called which is present under Global.asax file. Application Start method that registered the Route Table. A Route Table is a collection type that contains all the routes which are defined under the routeConfig files.
- UrlRoutingModule Intercepts the Request When first request made hit on browser URL then The UrlRoutingModule intercepts every request and find the correct route data. If UrlRoutingModule gets correct RouteData, it creates RequestContext object and forwards corresponding IRouteHandler(default id MvcHandler)
- MvcHandler Execution MvcHandler is responsible for initiate actual processing of ongoing request and generate response. MvcHandler gets information of current request through RequestContext object and passed to its constructor.
- Controller Execution MvcHandler uses the IControllerFactory instance and tries to get a IController instance. All Mvc controllers implement IController interface. This interface has Execute method which actually execute your action method code. So Mvc Controller executes the method from IController interface.
- Action Execution When a particular controller gets instantiated ActionInvoker determines which specific Action method needs to be execute. ActionInvoker uses ActionNameSelectorAttribute and ActionMethodSelectorAttribute to select Action method for execution.
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 ); }
When an action method return the ActionResult then execution pipeline selects appropriate ViewEngine to render ViewResult that is taken care by view engine's interface IViewEngine. ASP.NET MVC has Webform and Razor view engines. We can also create our custom view engines.
So, Guys This concludes the MVC request life cycle.
I Hope in this post I have covered all the points about MVC request life cycle which will be helpful to understand the concept of Asp.Net MVC request life cycle.
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery