What is the page Lifecycle of the MVC ?
Kumar Bhimsen
Actually, there is no concept of page in MVC. So this is not page life cycle but request life cycle.There are seven main steps that happen when you make a request to an Asp.net MVC web applications. 1. Routing Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table. When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests that match these patterns. An application has only one Route Table and this is setup in the Global.asax file of the application.public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute( "Default", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults); }2. MvcHandlerThe MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler implements IHttpHandler interface and further process the request by using ProcessRequest method as shown below: protected internal virtual void ProcessRequest(HttpContextBase httpContext) {SecurityUtil.ProcessInApplicationTrust(delegate {IController controller;IControllerFactory factory;this.ProcessRequestInit(httpContext, out controller, out factory);try{controller.Execute(this.RequestContext);}finally{factory.ReleaseController(controller);}}); }3. ControllerAs shown in above code, MvcHandler uses the IControllerFactory instance and tries to get a IController instance. If successful, the Execute method is called. The IControllerFactory could be the default controller factory or a custom factory initialized at the Application_Start event, as shown below: protected void Application_Start() {AreaRegistration.RegisterAllAreas();RegisterRoutes(RouteTable.Routes);ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory()); }4. Action ExecutionOnce the controller has been instantiated, Controller's ActionInvoker determines which specific action to invoke on the controller. Action to be execute is chosen based on attributes ActionNameSelectorAttribute (by default method which have the same name as the action is chosen) and ActionMethodSelectorAttribute(If more than one method found, the correct one is chosen with the help of this attribute).5. View ResultThe action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The result type can be ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.6. View EngineThe first step in the execution of the View Result involves the selection of the appropriate View Engine to render the View Result. It is handled by IViewEngine interface of the view engine. By default Asp.Net MVC uses WebForm and Razor view engines. You can also register your own custom view engine to your Asp.Net MVC application as shown below: protected void Application_Start() { //Remove All View Engine including Webform and RazorViewEngines.Engines.Clear();//Register Your Custom View EngineViewEngines.Engines.Add(new CustomViewEngine());//Other code is removed for clarity } 7. ViewAction method may returns a text string,a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and returns an HTML page to the browser by using the current view engine.
MVC page lifecycle can be define as follows :An instance of the RouteTable class is created on application start. This happens only once when the application is requested for the first time.The UrlRoutingModule intercepts each request, finds a matching RouteData from a RouteTable and instantiates a MVCHandler (an HttpHandler).The MVCHandler creates a DefaultControllerFactory (you can create your own controller factory also). It processes the RequestContext and gets a specific controller (from the controllers you have written). Creates a ControllerContext. es the controller a ControllerContext and executes the controller. Gets the ActionMethod from the RouteData based on the URL. The Controller Class then builds a list of parameters (to to the ActionMethod) from the request.The ActionMethod returns an in
MVC page lifecycle can be define as follows :-i) App Initialisation In this stage, the aplication starts up by running Global.asax’s Application_Start() method. In this method, you can add Route objects to the static RouteTable.Routes collection. If you’re implementing a custom IControllerFactory, you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property. ii) Routing Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern. MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table. iii) Instantiate and Execute Controller At this stage, the active IControllerFactory supplies an IController instance. iv) Locate and invoke controller action At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView(). v) Instantiate and render view At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.