In this article, I have explained about ASP.NET MVC application life cycle, basically how the asp.net mvc application get's request from user and then pass the results (View or response ) to user, after passing check of several stages.

Here is the basic image to understand the stages of MVC application life-cycle

Asp-net-mvc-application-life-cycle-stages-min.png

Let's understand each stage in detail:

Routing

When application starts, initially Application_Start method is called which is present in Glabal.asax. Using which we Register Route tables, Route table is basically a collection of route which are defined in routeConfig file

Route.Config File:

When you open RouteConfig.cs in your Project, you will see C# code as below, which is default route configuration 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 you will Build and run your Application in MVC

mvc-app-home-page.png

In this, when we run the application , by default /Home/Index method will get called.

http://localhost:62715/

http://localhost:62715/Home/Index

You can custom the route as per your need.

Custom Route 

routes.MapRoute(
             name: "about",
             url: "Home/About",
             defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
); 

 So for example if user hits url :  http://localhost:62715/Home/About , it will invoke HomeController and About Action Method.

URL Routing Module:

URL Routing module is responsible for mapping user request to particular controller actions.

As per user request, URL Routing Module will find URL in Route table to create RouteData Object.

If it finds correct match for request ,it will create RequestContext object and send request to MVCHandler. As soon as match is found, scanning of RouteTable process stops.

MVC Handler:

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

public MvcHandler(RequestContext requestContext)
{
}
      

It receives the information about the request from RequestContext object and passed to constructor, in implementation of GetHttpHandler() method in MVCRouteHandler class.

Controller Execution:

MVC controller implements IController interface. It has Execute method which calls particular action.

The "ControllerActionInvoker" determines which action to executed and executes the action

Action Result

As we are clear that, once the ActionMethod executes our logic we will return an ActionResult, based on the type of Result it returns, View generates accordingly.

Before that we do have some ActionFilter methods which is invoked based on ActionMethod’s life cycle. Some methods are such as OnActionExecuting, OnActionExecuted etc.

Let us see some of the ActionResult types as well,

  • ViewResult : Returns a view (html type page)
  • RedirectResult : Redirects to another action method
  • ContentResult : Returns Content (user defined)
  • JsonResult : Returns a serialized JSON data

If you do not want any specific method should not accessible through public URL, you can mark that method with [NonAction] attribute.

Rendering View :

The final step in MVC life cycle is rendering the View, where the user can see what he requested and get as a response.

The ViewResult one of the ActionResult generates the appropriate View to the user, by parsing the Razor syntax (cshtml) and server side codes into HTML page to the Client.

ViewResult implements the IViewEngine Interface and has some view manipulating methods such as:

  • ReleaseView : To release the View
  • FindView : Maps the appropriate View
  • FindPartialView : Returns a Partial View

Hope this article clears your basic knowledge of ASP.NET MVC life cycle architechture.

You may also like to read:

File Upload in MVC (Single or Multiple)

Understanding Delegates in C# (Single and Multicast with example)