Open In App

ASP.NET MVC Life Cycle

The goal of this article is to provide a good understanding of the MVC pipeline. The life cycle is basically is set of certain stages which occur at a certain time.

Application Life Cycle

MVC actually defined in two life cycles, the application life cycle, and the request life cycle. 



The application life cycle, in which the application process starts the running server until the time it stops. and it tagged the two events in the startup file of your application. i.e  the application start and end events

This is separate from the request life cycle, which is the sequence of events or stages that executed every time an HTTP request is handled by the application



MVC Request Life Cycle:

MVC Request Life Cycle

Lets we see now code end,

The MVC application is the main part of every MVC project. This class is inherited from HttpApplication Class. This class exposes a number of Request life cycle events that we can attach event handlers to and run our own code.

The application start and ends. These two events run before and after any other events and allow our application to start or stop receiving the requests.

Application Start :

so every MVC application life starts with an application start event. This event fires when the application receives its first request. This is the entry point file of the MVC application. This event fires when the application receives its first request.it allows us to perform global configuration before anything will happen which we can see default implementation here.

As you can see here,




using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
  
namespace MVC_Application {
    public class MvcApplication : System.Web.HttpApplication {
    protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(
                GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

Application End:

This event is fired when our application stops and then no more requests can be received.it is rare to use this event but if you want to run any task before the application completely shuts down intentionally. This is a good place to do it.

PreApplication Start:

There are some cases in which you want to execute some custom code before the application starts In this situation we have the option of using the PreApplication Start method. Actually, this method defines on assembly level.

Lets we see every phase in detail:

Controller Initialization:

MVC controller Initialization Flow

The MVCHandler’s,

Action Method Execution Process:

Action Method execution Flow

Next Phase,

Model Binding Process Flow

In-build, MVC provides action filters are:

Output Cache: This filter is used to caches the output of action for a certain duration of time.

Handle Error: This filter is used to handles the error caused by an action or controller, if any exception occurs it redirects the action to a custom error page.

Authorize: This filter is used for filtering the authorized user to access the resource.

Note: we can create a custom filter by implementing that particular filter interface class.

View  Result Execution Process:

View Result Execution Flow

Next Phase,

Action Result Execution Process:

Action Result Execution Flow

Next Phase,

That is all about MVC Life Cycle.


Article Tags :