asp net core the mvc request life cycle

ASP.NET Core: The MVC Request Life Cycle

Tuba Mansoor
Latest posts by Tuba Mansoor (see all)

I had earlier written a brief overview on the ASP.NET MVC request lifecycle here. In this article, we will discuss the ASP.NET Core – MVC request lifecycle.

While in ASP.NET MVC, a request is handled using HttpModules & HttpHandlers, ASP.NET Core MVC request lifecycle involves middleware. So what is middleware? Think of middleware as a series of blocks that are executed when a request is processed. If before proceeding you want to know more about middleware, this link from Microsoft is a good starting point.

When the application is started, it hits the Main method in program.cs.

asp net core the mvc request life cycle

This method then calls the ConfigureServices method in startup.cs. This is where all the required services in the application are added. Notice the AddMvc line.

See also  Events & Delegates in C#
asp net core the mvc request life cycle

The ConfigureServices method then calls the configure method, which calls the UseMvc middleware. This middleware then calls the routing middleware (UseRouter).

asp net core the mvc request life cycle

We can see a default route being created in this method. The routes specified in this UseMvc method are stored in a Routedata object.

When a request comes in, first attribute routes are evaluated and then conventional routes. Attribute routing is handled by MVCAttributeRouteHandler. Attribute routes are evaluated in parallel while in conventional routing the sequence of routing specified matters.

The MVCRouteHandler calls the RouteAsync method. This method calls the SelectCandidates method which selects all possible methods and checks if the entered url has a match in the Routedata .This is simply a name comparison. Then the SelectBestCandidates method is called which narrows down the choice based on action constraints like HttpPost and HttpGet. If a match is found then the particular controller and action method is selected.

The thing to note here is in non-core framework, the MVC Controllers inherited from the Controller class whereas the API controllers inherited from the ApiController class. With the Core framework, both types of controllers inherit from the same controller class.

The MVC RouteHandler decides the controller that needs to be invoked. The controller Factory class and the controller activator class create an instance of the controller and an action method is selected..

See also  Performance Consideration for C# Conditional Statements

Before the controller is created, authorization filters are called. If the authorization is successful, then the controller is created. Once the controller is created and action method selected, model binding takes place. This is responsible for populating the action method parameters using querystrings, form data, route data etc.

After the model binding takes place, OnActionExecuting filter is executed. Action method is executed. Then the OnActionExecuted filter is executed. Action result is now generated. Remember that in MVC there is a difference between action result generation and the actual rendering of the action result.

After this OnResultExecuting filters are executed next and action result is invoked. After this OnResultExecuted filter is executed. This is the last stage where action result can be modified.
Since in ASP.NET core, the MVC lifecycle and Web API lifecycle are now integrated into one, content negotiation also takes place when creating ActionResult.

After this action result like JsonResult, ViewResult etc are rendered, depending upon the action result that needs to be created.

The diagram below outlines the steps that we have discussed above.

If you are interested in delving further into this topic, the ASP.NET Core: The MVC Request Life Cycle course on Pluralsight by Alex Wolf is an excellent place to begin.

See also  Design Pattern : Adapter