Introduction to ASP.NET MVC Life Cycle
ASP.NET MVC Life Cycle is the request like how the request is processed as it passes from one to another. It is processed from the components in order which they occur in Life Cycle Application. It explains the roles of each and every component and each component how they related to every component in the pipeline.
ASP.NET MVC Life Cycle Overview
In ASP.NET MVC Life Cycle, there are five steps that happen to make the request from ASP.NET MVC let’s see the following steps below,
- Initially, RouteTable is created, it happens once when the application starts first and foremost the RouteTable maps the urls to handlers.
- UrlRoutingModule Intercepts Request happens when we make the request. Whenever the URLRoutingModule intercepts a request it creates and executes the right handler.
- MVCHandler Executes, it creates the Controller and it passes the Controller as a ControllerContext and finally, executes the Controller.
- Controller Executes, it decides the Controller method for the execution and builds the list of parameters, and executes the method.
- Called by Method RenderView, the Controller Method calls RenderView() for rendering the content returns to the browser. Controller RenderView() method delegates their work to a particular ViewEngine.
ASP.NET MVC Life Cycle Process
In MVC Application there will be no physical page exists for particular requests. Entire requests are routed to the particular class which is called the Controller. Controller is dependable for creating the responses and transmitting the content returned to the browser. There will be a many-to-many mapping between Controller and URL. While sending the request to MVC Application we can directly make a call to Action Method for the Controller.
For example, if we give http://domain_name/Controller_1/Method_, this explains that you are calling the Controller_1’s
Methods_1. Here we see the request routing to ActionMethod of Controller.
Let’s see the procedures involved in,
- While the Application starts the instance of RouteTable class is created. It happens when the application is requested initially.
- URLRoutingModule intercept each and every request and checks the matching of RouteData from RouteTable and instantiates MVCHandler it is the HttpHandler.
- DefaultControllerFactory created by MVCHandler we can also create our own Controller Factory. It then processes the RequestContext and retrieves the particular Controller. It creates ControllerContext for the execution of the Controller.
- It retrieves the ActionMethod from theRouteData depending on the URL. The Controller Class creates the list of parameters from the requests.
- The ActionMethod gives back the instance of a class inherited from ActionResult class and renders the View Engine for the View as Web Page.
ASP.NET MVC Life Cycle Two Request
The ASP.NET MVC Request Life Cycle initiates from birth to the death of a request. Let’s see the two requests as follows,
- Application Life Cycle
- Request Life Cycle
Request Life Cycle – ASP.NET MVC Application
Let’s understand the Request Life Cycle of ASP.NET MVC Application. Look at the following diagram which explains the high-level architecture of the MVC- Request Processing Pipeline.
When the request is raised by the client and it is the first request to the application then the Application_Start event going to execute and the events start working on it. Briefly, the Application_Start event makes the call of RegisterRoutes Method of RouteConfig class which covers the RouteTable with routes defined in the application. Then request comes to the Routine module.
ASP.NET MVC Life Cycle Application
In ASP.NET MVC Application Life Cycle explains that we retrieve the request from the user and then it returns back the results to the user (which is called View or Response) once passing it to check several stages. Let’s understand the stages of the MVC Application Life Cycle.
Let’s see each and every stage,
Routing – initially when the application begins Application_Start method is called which is present in the Global.asax. We registering the RouteTable it is the collection of Routes defined in routeConfig file.
Route. Config File – While opening RouteConfig.cs lets see the default routing config in MVC
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
When executing the application in MVC by default the http://localhost:—-/Home/Index. Let’s see the custom routing also,
Custom Route
routes.MapRoute(
name: "about",
url: "Home/About",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
This routing will be like http://localhost:—-/Home/About, the invoking method is like HomeController and then About Action Method.
URL Routing Module
It is responsible for mapping the user request to specific Controller Actions. By the user request the URL Module searching the Routing table to create RouteData Object. It checks the exact match for the request which creates RequestContext Object and sends the request to MVCHandler. Once founding the match it then scans the RouteTable process stops.
MVC Handler:
The MVCHandler responsible for initiating the actual process of request and creating the responses. It retrieves the information of the current request by RequestContext Object passed to Constructor.
public MvcHandler(RequestContext requestContext)
{
}
It gets the information about requests from the object of RequestContext and it passes to the constructor and it implements in GetHttpHandler() Method in MVCRouteHandler class.
Controller Execution
In this, the MVC Controller implements the IController Interface and it executes a method which makes a call for specific action. The ControllerActionInvoker defines the actions to be executed and executes the action.
Action Result
In this only we execute the ActionMethod which executes the Logic which returns the ActionResult based on the ResultType it returns. ActionFilter Methods invoked based on the ActionMethod Life Cycle, methods like OnActionExecuting, OnActionExecuted, and so on. There are several ActionResult types they are as follows,
- ViewResult
- RedirectResult
- ContentResult
- JsonResult
Rendering View
The final step is the MVC Life Cycle Rendering View where users can able to see what they requested and get back as a response. ViewResult is the one ActioResult that builds a suitable view to the user by sending Razor Syntax (cshtml) and server-side codes on the HTML page. The ViewResult implements the IViewEngine Interface and has view methods such as,
- FindView
- FindPartialView
- ReleaseView
Conclusion
In this article, I have explained the complete life cycle of ASP.NET MVC requests from birth to death. It includes the steps involved in processing requests of ASP.NET MVC like creating executing the action, generating Controller, Rendering View, and so on. Hope the article clears up the basic knowledge in ASP.NET MVC Life Cycle.
Recommended Articles
This is a guide to ASP.NET MVC Life Cycle. Here we discuss the introduction, Overviews, ASP.NET MVC Life Cycle Application, ASP.NET MVC Life Cycle Two Request along with examples and syntax. You may also have a look at the following articles to learn more –