MVC Life Cycle
A life cycle is only a sequence of actions or occurrences used to respond to a specific request or modify the status of an application.
MVC has two life cycles −
The Application Life Cycle
When an ASP.Net application is launched, there are series of steps which are carried out. These series of steps make up the lifecycle of the application.
It covers the period from when the application process really starts, running IIS until it ends. The application start and finish events in your application's startup file serve as a marker for this.
The various stages of a typical page lifecycle of an ASP.Net Web Application.1) Application Start – The life cycle of an ASP.NET application starts when a request is made by a user. This request is to the Web server for the ASP.Net Application. This happens when the first user normally goes to the home page for the application for the first time. During this time, there is a method called Application_start which is executed by the web server. Usually, in this method, all global variables are set to their default values.
2) Object creation – The next stage is the creation of the HttpContext, HttpRequest & HttpResponse by the web server. The HttpContext is just the container for the HttpRequest and HttpResponse objects. The HttpRequest object contains information about the current request, including cookies and browser information. The HttpResponse object contains the response that is sent to the client.
3) HttpApplication creation – This object is created by the web server. It is this object that is used to process each subsequent request sent to the application. For example, let’s assume we have 2 web applications. One is a shopping cart application, and the other is a news website. For each application, we would have 2 HttpApplication objects created. Any further requests to each website would be processed by each HttpApplication respectively.
4) Dispose – This event is called before the application instance is destroyed. During this time, one can use this method to manually release any unmanaged resources.
5) Application End – This is the final part of the application. In this part, the application is finally unloaded from memory.
ASP.Net Page Life Cycle
An ASP.Net page has a specific lifetime when it is called. Before the user receives the response, this is done. For the processing of an ASP.Net page, a sequence of actions are taken.
Page Request– At this point, the server receives the first request for the page. The server determines if it is the first time the page has been requested when it receives a request. If so, it must put the page together, analyse the response, and transmit it to the user. The cache is examined to see if the page output exists if it is not the first time the page has been requested. If so, the user receives that response.
Page Start – Two objects—the Request and Response objects—are formed at this point. All the data that was sent when the page was requested is stored in the Request object. The data that is delivered back to the user is stored in the Response object.
Page Initialization – All of the controls on a web page are initialised during this period. As a result, any labels, text boxes, or other controls you may have on the web form are all initialised.
Page Load – This is when all of the default values are actually loaded onto the page. Therefore, if a textbox is supposed to have a default value, that value loads as the page loads.
Validation – The form may occasionally have some validation set. A validation might specify, for instance, that a list box must have a particular set of values. There should be a problem loading the page if the condition is false.
Postback event handling – If the same page is loaded once more, this event is started. In response to a previous occurrence, this occurs. On sometimes, it may happen that a user clicks the page's submit button. The same page is shown once more in this instance. The Postback event handler is invoked in this situation.
Page Rendering – This occurs immediately before the user receives all of the response data. The form's whole contents are saved, and the user receives the finished product in the form of an entire web page.
Unload – The ASP.net web form objects don't need to be kept in memory once the user receives the page output. In order to unload, all unnecessary items must be deleted from memory.