In this section, I am going to describe MVC Life Cycle. If you are an MVC developer, then this entire flow should be in your DNA. This not only solves complex problem rather it also makes an app more robust, future proof, maintainable and testable. Below, I have pasted very high level diagram for the same. Therefore, without wasting time, let us get started.
Entry point for any MVC application begins with routing. After that ASP.NET platform receives the request and figures out how it should be handled by URL routing modules. The routing module is responsible for matching incoming URLs. All routes are associated with route handlers. So, if request matches the defined route then MVC route handler will create an instance of MVC HTTP handler and retrieves the requested controller. MVC routehandler and MVC HTTPHandler are two classes and together they act as a gateway into the MVC Framework.
Then, MVC handler begins the process of initializing and executing the controller. MVC framework handles converting our route data into concrete controller which can handle request. This is accomplished via controller factory and activators which is responsible for creating controller instance. This is also the place where features like Dependency injection can be injected.
After the controller is created, next step is action execution. A component called action invoker finds and selects the appropriate action method to invoke on our controller. Now, before this step model binding takes place which actually maps the data from HTTP request to parameters on our action methods. Action filters are also called before and after the method produces an action result.
Now, after Action Result takes place, next step is result execution. MVC separates declaring the result from executing the result. so, if result is view type then view engine will be called and its responsible for finding and rendering our view. If the result is not view, then action result will execute on its own. This result execution actually generates an actual response to that original HTTP request. So, this was the high level stuff.
Now, let us jump in the demo section. I have created a simple MVC 5 app as shown below in the screenshot.
One important point to understand here, that @ the heart of any MVC application class “MvcApplication” sits; you can find the same in global.asax file.
Now, this class inherits from ASP.NET “HTTPApplication” class. Now, HTTP application exposes a number of life cycle events which we can attach with event handlers and run our code. These events are:-
Therefore, every MVC application starts with Application_Start event. This event fires when the app receives it’s first request and also allows us to perform global configurations before anything else gets started. I have already pasted one snippet above. This gives you the glimpse of various setting, which we are doing before any MVC application starts. Here, we are also registering routes. Now, when you see the default implementation for the same, you will find below code for the same.
Basically, RegisterRoutes method adds any route to a static collection of RouteTable class. Now, this is the collection of routes which URL Routing module will use to match any incoming URLs. Also, each of theses routes are also associated with RouteHandler class. RouteHandler will provide HTTPHandler which will actually process incoming request. Therefore, routes are registered inside route table before anything else happens at the Application_Start event. Below I have pasted screenshots while debugging the app.
As, we can see in the above screenshot, while running the app in debug mode, it first hit the Application_Start event. Once, done with all global configurations, it then produced the default template for MVC app as shown below.
In order to test, the Application_End event, I just need to stop the IIS Express from the tray icon as shown below.
Once, I clicked on Exit, my breakpoint hit.
Now, upon successful execution, it also printed the same message in the Output window as shown below.
I hope you would have liked this small demo around MVC Life Cycle. We will delve further in coming section. Till then stay tuned and Happy Coding.
Disclaimer:- This flow is valid upto MVC 5.