• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, July 18, 2017

Page Life cycle of Asp.Net MVC

 Abhishek Tomer     July 18, 2017     Asp.Net, Asp.Net MVC, C#     1 comment   

In this article I am going to explain what exactly happens in ASP.NET MVC request life cycle and what each step in the life cycle does and how we can leverage it further based on our needs. This article specifically targets Page life cycle which is different from Application life cycle. A typical Application life cycle contains Application start and Application End events, however http Life cycle is something which is repeated for every request. Since application events are also part of life cycle, we will see them as we move along.

The MVC Request Life Cycle
The entry point of MVC Request life cycle is URL Routing module, the incoming request from IIS pipeline is handed over to URL Routing module which analyses the request and looks up Routing table to figure out which controller the incoming request maps to.


"Routing Table is a static container of routes defined in MVC application with corresponding controller action mapping. If the route is found in the routing table MVCRouteHandler executes and brings the instance of MVCHttpHandler".


Together they act as a gateway into the MVC Framework. MVC handler begins initializing and executing controller. The MVCHttpHandler also takes of converting route data into concrete controller that is capable of serving the request. MVC handler does all this with the help of MVC Controller factory and activator which are responsible for creating an instance of the controller. This is also the place where the Dependency Injection is performed if the application has been designed to invoke parameterized controller constructor and satisfy its dependencies.


After the controller instance is created the next major step is to find and execute the corresponding action. A component called ActionInvoker finds and executes the action defined in routing table. Before the action method is called model bindings takes place which maps data from http request to action method parameters. After the model binding, action filters are invoked which includes OnActionExecuting filter. This is followed by action execution and Action Executed filter execution and finally preparing Action Result.

Once the Action method has been finished executing the next step is Result execution. MVC separates the action declaration from Result execution. If the Result from action execution is view, then depending upon configuration, ASPX or Razor view engine will be called to find and render the html view as a response of http request. If the result was not view then it’s passed as-is to http response.

Following is the conceptual view of MVC request life cycle.
Page Life cycle of Asp.Net MVC
Page Life cycle of Asp.Net MVC

WebForms vs MVC
If you have ever developer a web forms application, then this is important scenario to understand about the difference between both.

  • In Web forms life cycle, service corresponds to the physical file. However in MVC, this concept is completely different as Controller Actions are executed to render a view to the view.
  • Inspite of having the differences, both Web forms and MVC requests are implemented through HttpHandler. In web forms each page is derived from IHttpHandler interface and request is served more directly. MVC controllers are also derived from HttpHandlers. From a higher level, MVC is just another way to manage request from ASP.net platform.
"One can create their own pipeline and build a framework like MVC or web forms on their own."
Pipeline for Asp.Net MVC Application
Pipeline for .Net Application

There are five main steps that happen when you make a request from an ASP.NET MVC website.
  1. Route Table Execution
  2. When application is run first time Application_Start method is called which is present under Global.asax file. Application Start method that registered the Route Table. A Route Table is a collection type that contains all the routes which are defined under the routeConfig files.
    public static void RegisterRoutes(RouteCollection routes)
     {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute("Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
     }

  3. UrlRoutingModule Intercepts the Request
  4. When first request made hit on browser URL then The UrlRoutingModule intercepts every request and find the correct route data. If UrlRoutingModule gets correct RouteData, it creates RequestContext object and forwards corresponding IRouteHandler(default id MvcHandler)

  5. MvcHandler Execution
  6. MvcHandler is responsible for initiate actual processing of ongoing request and generate response. MvcHandler gets information of current request through RequestContext object and passed to its constructor.

  7. Controller Execution
  8. MvcHandler uses the IControllerFactory instance and tries to get a IController instance. All Mvc controllers implement IController interface. This interface has Execute method which actually execute your action method code. So Mvc Controller executes the method from IController interface.

  9. Action Execution
  10. When a particular controller gets instantiated ActionInvoker determines which specific Action method needs to be execute. ActionInvoker uses ActionNameSelectorAttribute and ActionMethodSelectorAttribute to select Action method for execution.

    When an action method return the ActionResult then execution pipeline selects appropriate ViewEngine to render ViewResult that is taken care by view engine's interface IViewEngine. ASP.NET MVC has Webform and Razor view engines. We can also create our custom view engines.
Summary:
So, Guys This concludes the MVC request life cycle.

I Hope in this post I have covered all the points about MVC request life cycle which will be helpful to understand the concept of Asp.Net MVC request life cycle.

Please share this post with your friends and colleagues.

For any queries please post a comment below.

Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post

1 comment:

  1. JanuJune 9, 2020 at 2:24 PM

    Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work





    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery



    ReplyDelete
    Replies
      Reply
Add comment
Load more...

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape