Updated June 29, 2023
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 the order in Life Cycle Application. It explains the roles of each component and how they relate to every component in the pipeline.
ASP.NET MVC Life Cycle Overview
In ASP.NET MVC Life Cycle, five steps happen to request 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 creates the Controller, passes the Controller as a ControllerContext, and executes the Controller.
- Controller Executes, it decides the Controller method for the execution, builds the list of parameters, and executes the method.
- Called 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, the Controller. The 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 the 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 the ActionMethod of the Controller.
Let’s see the procedures involved –
- When the Application starts, the instance of RouteTable class is created. It happens when the application is requested initially.
- URLRoutingModule intercepts every request and checks the matching of RouteData from RouteTable and instantiates MVCHandler it is the HttpHandler.
- MVCHandler created the DefaultControllerFactory, and 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 the ASP.NET MVC Application. Look at the following diagram, which explains the high-level architecture of the MVC- Request Processing Pipeline.
When the client raises the request, which is the first request to the application, the Application_Start event will execute, and the events start working on it. Briefly, the Application_Start event calls the 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 the results to the user (View or Response) once passing it to check several stages. Let’s understand the stages of the MVC Application Life Cycle.
Let’s see every stage –
Routing – initially, when the application begins Application_Start method is called, which is present in the Global.asax. The RouteTable is the collection of Routes defined in the routeConfig file.
Route. Config File – While opening RouteConfig.cs let’s 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’s request, the URL Module searches 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 funding the match it then scans the RouteTable process stops.
MVC Handler:
The MVCHandler is responsible for initiating the actual process of requests 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 passes it to the constructor, and it implements in GetHttpHandler() Method in MVCRouteHandler class.
Controller Execution
In this, the MVC Controller implements the IController Interface and executes a method that calls for specific action. The ControllerActionInvoker defines and executes the actions.
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 see what they requested and get back 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 and executing the action, generating Controller, Rendering View, and so on. I hope the article clears up the basic knowledge in ASP.NET MVC Life Cycle.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET MVC Life Cycle” was beneficial to you. You can view EDUCBA’s recommended articles for more information.