Page life cycle of MVC

Page life cycle of MVC is like below .When Browser request to application it will do first

1)App initialization: It will create initialization of Application of IIS

2)Routing: It is very important of MVC , this is the first step , all mvc based application starts with routing,it passes UrlRoutingModule , the route table contains all registered routing data, it passes through global.asax, it searches the pattern of url , When a match is found, it retrieves the IRouteHandler object for that route.

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
);
}

3)MVC handler: this application is responsible for mvc application initialization, The MvcRouteHandler will create an instance object of mvchandler and will always passes through the requestcontext, this handler mainly inherited from ITttpHandler .

4)Controller: for using controller it will use the Icontrollerfactory , it can be used in application_start.

5)Action Invocation: After controller instantiating , action invocation done.

6)Viewresult:for diffrent view result return after action invocation , this is for responsible.there are different type of view result like below

a)ViewResult

b)PartialViewResult

c)RedirectResult

d)RedirectToRouteResult

e)ContentResult

f)JsonResult

7)View Engine:View engine mainly responsible for render the data in html based on selecting view engine , there are different types of view engine (Razor, ASPX, Spark and NHaml) , in mvc 2.0 thre are aspx view engine, but from MVC 3.0 there was new view engine from microsoft called Razor view engine, which has advantages of Unit testing , there ais possibility of creating custom view engine ,

 

public class MyCustomViewEngine : IViewEngine
{

    public CustomViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, booluseCache)
{
//Finds the partial view by using the controller context.
return null;
}

    

}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
//Clear all current register view engine
ViewEngines.Engines.Clear();
//Register Custom View Engine
ViewEngines.Engines.Add(new MyViewEngine());
RegisterRoutes(RouteTable.Routes);
}
}

8)View: An Action method may return JSON data, a simple string or file. commonly it will return a view result or partial view result.

You Might Also Like

Leave a Reply