Asp.net MVC request life cycle

by Ashwini Verma  |  10 June, 2016  |  Views : 2949

As any other web application technology, Asp.net MVC also create a request object and response back to browser in html format. In short, request process starts when we call the controller and action method from browser.

For example : www.mydomain/home/index

If you see below picture, this is normal flow. Request goes to appropriate controller's action and render view.

Now let's discuss in depth. Below picture depicts Asp.net MVC request life cycle.

RouteTable : It is a class which stores the URL routes for an application. It uses a RouteCollection to store URL routing information. You will have more clear picture if you see the code below. RouteTable is registered in Application_Start() method inside Global.asax.cs file.

protected void Application_Start()
{
      RouteConfig.RegisterRoutes(RouteTable.Routes);
}
      

RouteTable contains RouteCollection class and these routes is added or mapped in RouteConfig.cs file.

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 }
            );
        }
        

Hence, when request is created, controller and action pattern is searched in RouteTable, if pattern matches MVCHttpHandler instance gets created to execute relevant controller.

MVC Handler : It is a class which implement IHttpHandler which is responsible to handle ongoing request further. It create controller instance inside ProcessRequest() method of the MvcHandler.

Controller : ControllerBase class is a base class of controller which implement Icontroller interface. It has Execute() method which gets the action from the RouteData based on the URL.

Action Result : Controller class then calls ActionInvoker which builds a lists of parameter from request and execute the Action. ActionResult is abstract class which is a base class for all action results.

View Engine : The view engine is responsible for creating HTML from your views. There are two type of view engines provided by Asp.net MVC; ASPX and Razor. View engine selection option is provided while creating project template.

View Result : View rendering process starts after View Result call. This class identify which view to render using FindView() method.

I would like to know if any question or feedback. You can write your comment below.


posted on : 10-06-2016 14:55:08 viewed : 2949

Follow Us

Latest News