Wednesday, April 11, 2018

MVC Application Life Cycle

1. Routing  :- It is the first step in MVC request life cycle. When a route match than the routing engine
forwards the request to the corresponding IRoutHandler for that request. The default one calls the MvcHandler.

2. MvcHandler :- It is the reponsible for starting the real processing. MVC Handler implements IHttpHandler interface and
process the request using ProcessRequest method.

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.Contoller :- As depicted above, Mvc Haanlder uses the IControllerFactory instance and tries to get a IController
instance.If successful, Execute method is called. IControllerFactory could be the defualt 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 Execution :- After Controller instantiated, Controller's ActionInvoker determines which
specific action to invoke in the controller. Action method is chosen based on attributes ActionNameSelectorAttribute
(method name if not specify) ActionMethodSelector( If multiple method found with same name, the correct one chosen based on this attribute)

5.View Result :- The action method receives user input, prepare response data and than execute the result
by returning a result type. The result type could be ViewResult,RedirectToRouteResult,RedirectResult,ContentResult
,JsonResult,FileResult, adn EmptyResult.

6. View Engine :- The first step in the execution of the ViewResult involves the selectin of the appropriate View Engine to render the View Result.
It is handled by IViewEngine interfce of the view engine. By default MV uses WebForm and Razor view Engines. You can also register your own custom view engine.

protected void Application_Start() 

 //Remove All View Engine including Webform and Razor
 ViewEngines.Engines.Clear();
 //Register Your Custom View Engine
 ViewEngines.Engines.Add(new CustomViewEngine());
 //Other code is removed for clarity
}

7. View :- Action method may return a text string, a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and return
an HTML page to the browser by using current view engine.

No comments:

Followers

Link