MVC Life Cycle Explained

1) An application has one and only one Route Table. This Route Table is setup in the Global.asax file
Initially Application_Start() event is called for the application, where we set all the Routes in the static RouteTable.Route collection (which is of type RouteCollection)

 "Default", // Route name
 "{controller}/{action}/{id}", // URL with parameters
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }

All route object map route Url to MVCRouteHandler which implement IHttpHandler.

2) The UrlRoutingModule class matches an HTTP request to a route in an ASP.NET MVC application.
UrlRoutingModule passes HttpContext object to RouteTable. The HttpContext object includes Url, form parameters, query string, cookies with the current request.

ControlFlow of MVC

3) The MapRoute method being invoked inside of the RegisterRoutes method is an extension method. Every route registered with MapRoute is configured to use MvcRouteHandler, which is a RouteHandler provided by MVC framework.
It is route handler job to find the HTTP Handler (which implement IHttpHandler) for a request, in MVC this handler is called MvcHandler.

4) When control reaches this MvcHandler, it extract the controller parameter from RouteData and send them to ControllerFactory. It is the factory responsibility to return a controller. All controller in MVC implement IController.
MVC framework provide a default controller factory named DefaultControllerFactory that will search assemblies in appdomain to the Controller with implement IController & ends with Controller. Then the same instance is returned to the MvcHandler. When MvcHandler has a IController reference, it invokes the Execute() on controller.

5) The Execute() method extract members from the Request object and some of which act as method parameter.
The Execute() method finds method of the controller by using reflection on Controller class. It finds the method (action) which matches the route and executes it.

6) When controller transfer to RenderView() method, the call is delegated to ViewEngine’s RenderView() method. It uses ViewLocator class to find the View. And all master page, ViewBag, ViewData are set.

References :



Filed under C#

2 responses to “MVC Life Cycle Explained

  1. sreedhar

    Thanks for this

Leave a Reply to gurunguns Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s