See this article on the Internet, the foreigner wrote, many of the pictures are very delicate, incidentally translated to share under, English too many places can not turn over
ASP. Application and Page life cycle
Download Source code-4.03 KB
Directory
- Brief introduction
- Two-Step processing method
- Creating an ASP. NET Environment
- Handling requests using the MHPM event
- What events are we in?
- A simple demo code
- Zooming in on an ASP page event
- About source code
- References
Brief introduction
In this article, we will try to figure out the different events that take place from the beginning of the user sending the request to the completion of the browser rendering process. So we need to figure out two big phases of the ASP.
Then we will go into the object from different events to trigger HttpHandler
the "," HttpModule
and ASP. In this journey of events, we need to understand the logical process of each event
This ebook is suitable to cover. NET such as Wcf,wpf,wwf,ajax,core.net,sql and so on knowledge points, you can download the same content from here or you can chase my daily free course from here.
Two-Step processing method
From a height of 30000 feet (from the 30,000 feet level, the author is not joking), the ASP. NET request processing is done in two steps, as follows, the user sends a request to IIS:
- Asp. NET creates an environment that can handle the request, in other words, it creates application objects, requests, will act, and context objects to handle the change request
- When the environment is created, the request is processed by a series of events from the use of modules (by using modules), processor (handlers), and Page objects, and to simplify, we name this stage MHPM (module,handler,page and Module event), we'll talk about it in a minute.
In the coming sections, we'll understand both these main steps in more detail.
Creating an ASP. NET Environment
Step 1: The user sends a request to Iis,iis (process Inetinfo.exe) to first check which ISAPI can handle this request. This is determined by the extension of the file, for example, if the page is an. aspx page, then it will be handled by 'aspnet_isapi.dll'
Step 2: If this is the first request to be sent to this site, then a class named " ApplicationManager
will create an application domain where the site is running." It is well known that application domains from different site programs are isolated from each other in the same IIS. So if there is a problem in one app domain, it will not affect other app domains.
Step 3: The newly created app domain creates a managed environment (hosting environment), for example, " HttpRuntime
object." Once the hosting environment is created, the required core ASP. NET objects such as ' HttpContext
, ' and HttpRequest
' are HttpResponse
also created.
Step 4: once all the ASP. NET core objects are created, the HttpApplication
object is created to serve the user's request, if your system contains a 'Global.asax' file, then ' The Global.asax' file object is created. Keep in mind that the Global.asax file is built into the ' HttpApplication
class '.
Note: The first time an ASP. NET page is connected to an app, a new ' HttpApplication
instance is created, in order to maximize performance, ' HttpApplication
The instance can be reused for multiple requests
Step 5: then HttpApplication
the object is assigned to the ASP. NET core object to process the page.
Step 6: HttpApplication
Requests are then processed through HTTP module events, processors, and page events. It triggers the MHPM event for request processing
Note: Want to know too much, read this.
The following picture illustrates the relationship between those internal object modules and the ASP. ASP. When the top-most layer is created Appdomain
, the
' Appdomain
contains ' request ', ' response ' and ' context ' object ' HttpRuntime
Handling requests using the MHPM event
Once HttpApplication
the ' creation is complete, it starts processing the request. It went through three different stages. It invokes different events through these stages, in which the developer can extend the code to add their own logic inside.
Before we go on, let's figure out what is " HttpModule
and" HttpHandlers
. They helped us inject the custom logic before and after the ASP, the main differences are as follows:
· If you want to inject logic based in file extensions like '. ASPX', '. HTML', then your use ' HttpHandler
. In the other words, "is an HttpHandler
extension based processor.
· If you want-inject logic in the events of the ASP. NET Pipleline, then you use ' HttpModule
. ASP. In the other words, "is an HttpModule
event based processor.
If you want to know more, poke here.
The following is a logical process of request processing, which shows 4 important steps:
Step 1 (m:httpmodule): customer request processing begins, and before the ASP is run and created, ASP. NET HttpModule
provides events that can inject custom logic, which provides 6 important events that you can use to create a Page object BeginRequest
, AuthenticateRequest
,,, AuthorizeRequest
ResolveRequestCache
AcquireRequestState
and PreRequestHandlerExecute
before.
Step 2 (H: ' HttpHandler '): when the above 6 events are executed, ASP. NET engine then triggers the event, if your project has execution HttpHandler
, ASP. NET engine will invoke the ProcessRequest
event.
Step 3 (P:asp.net page): when HttpHandler
the logic is executed, the current ASP. NET Page object is created. When an ASP. NET Page object is created,
Some events are triggered, and we can write custom logic in these page events, providing 6 important events in ASP. We can write custom logic.
Page Init
,, Load
, validate
event
, and render
unload
. You can use words SILVER
to memorize these events, S-Start (no special meaning in this word), i-init,l-load,v-validate, E-event,r-render
Step4 (m:httpmodule): When the Page object executes and unloads from memory, HttpModule
provides post-page processing events that we can use to handle page logic.
It has 4 important post-processing events,, PostRequestHandlerExecute
ReleaserequestState
UpdateRequestCache
and EndRequest
.
The picture below illustrates the process just now.
What events are we in?
The Million dollar question comes, what should we do in an event? The following table lists the code that performs what is logically handled in what event.
Section |
Event |
Description |
HttpModule |
BeginRequest |
This event signals a new request; It is guaranteed to being raised on each request. |
HttpModule |
AuthenticateRequest |
This event signals the. asp. NET runtime is ready to authenticate the user. Any authentication code can is injected here. |
HttpModule |
AuthorizeRequest |
This event signals the. asp. NET runtime is ready to authorize the user. Any authorization code can is injected here. |
HttpModule |
Resolverequestcache |
In ASP., we normally use OutputCache directive to do caching. In this event, ASP. Determines if the page can be served from the cache rather than loading the patch from SCRA Tch. Any caching specific activity can is injected here. |
HttpModule |
AcquireRequestState |
This event signals. ASP. NET runtime is the ready to acquire session variables. Any processing your would like to does on session variables. |
HttpModule |
PreRequestHandlerExecute |
This event was raised just prior to handling control to the HttpHandler. Before you want the control to being handed over to the handler any pre-processing your would like to do. |
HttpHandler |
ProcessRequest |
HttpHandler logic is executed. In this section, we'll write logic which needs to be executed as per page extensions. |
Page |
Init |
This event happens in the ASP. Page and can used for: · Creating controls dynamically, in case you had controls to is created on runtime. · Any setting initialization. · Master pages and the settings. In this section, we don't have access to ViewState, postedvalues and neither the controls is initialized. |
Page |
Load |
In this section, the ASP. Fully loaded and you write UI manipulation logic or any other logic over here. |
Page |
Validate |
If you had valuators on your page, you would like to check the same here. |
|
Render |
It's now time-to-send the output to the browser. If you would like to make some changes to the final HTML which are going out to the browser and you can enter your HTML logic Here. |
Page |
Unload |
Page object is unloaded from the memory. |
HttpModule |
PostRequestHandlerExecute |
Any logic-would like to inject after the handlers is executed. |
HttpModule |
ReleaseRequestState |
If you would like to save update some the state variables like session variables. |
HttpModule |
Updaterequestcache |
Before you end, if you want to update your cache. |
HttpModule |
EndRequest |
The last stage before your output was sent to the client browser. |
A simple demo code
With this article, we had attached a sample code which shows how the events actually fire. In the This code, we have created a "and" in this HttpModule
Httphandler
project and we have displayed a simple response write in all E Vents, below is what the output looks like.
Below is the class for " HttpModule
which tracks all events and adds it to a global collection.
public class Clshttpmodule:ihttpmodule
{
......
void Onupdaterequestcache (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onupdaterequestcache");
}
void Onreleaserequeststate (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onreleaserequeststate");
}
void Onpostrequesthandlerexecute (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onpostrequesthandlerexecute");
}
void Onprerequesthandlerexecute (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onprerequesthandlerexecute");
}
void Onacquirerequeststate (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onacquirerequeststate");
}
void Onresolverequestcache (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onresolverequestcache");
}
void Onauthorization (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:onauthorization");
}
void OnAuthentication (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:authenticaterequest");
}
void OnBeginRequest (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:beginrequest");
}
void Onendrequest (object sender, EventArgs a)
{
Objarraylist.add ("Httpmodule:endrequest");
Objarraylist.add ("
foreach (String str in objarraylist)
{
HttpApp.Context.Response.Write (str + "<br>");
}
}
}
Below is the code snippet for ' HttpHandler
which tracks ' ProcessRequest
event.
public class Clshttphandler:ihttphandler
{
public void ProcessRequest (HttpContext context)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Httphandler:processrequest");
Context. Response.Redirect ("default.aspx");
}
}
We is also tracking all the events from the ASP.
public partial class _default:system.web.ui.page
{
protected void Page_Init (object sender, EventArgs e)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:init");
}
protected void Page_Load (object sender, EventArgs e)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:load");
}
public override void Validate ()
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:validate");
}
protected void Button1_Click (object sender, EventArgs e)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:event");
}
protected override void Render (HtmlTextWriter output)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:render");
Base. Render (output);
}
protected void Page_Unload (object sender, EventArgs e)
{
CLSHTTPMODULE.OBJARRAYLIST.ADD ("Page:unload");
}}
Below is, the display looks like with all events as per the sequence discussed in the previous section.
Zooming in on an ASP page event
In the previous section, we browsed through the complete process of an ASP. NET page request. One of the most important is that the section is an ASP. We haven't discussed its details yet, so let's take some effort in this section to describe the more details of the ASP.
Some ASP. NET has 2 parts, part of the HTML tag that is displayed on the browser, and another ViewState hidden field of the form that is stored on the HTML INPUT element. When the page is published, the HTML tags are created on the server side, with the ASP. ViewState and form data Bundle (original: These HTML tags is created in to ASP) with ViewState a ND form data tied up together on the server, this translation is very awkward, their original understanding of it), we can get all the server-side control in the background code, you can execute and write the custom logic and then render the page to the browser
For now, for these HTML controls that are connected to a server as an ASP. NET, ASP. NET provides a number of events that are available for our custom logic. We just need to put the tasks or logic we want to define in the right event. (Original: Now between these HTML controls coming live on the server as ASP., the ASP. Emits out lot of event s which can is consumed to inject logic. Depending on anything task/logic you want to perform, we need to put the this logic appropriately in those events. There is a problem with this translation. )
Note: Most developers use it page_load
to handle everything directly. That's not a good idea. So, it both fills the control, sets the view state, sets the theme, and so on, and every thing happens on page load. So, if we can put the custom logic into the appropriate event according to the natural attributes of the event, then our code will look more refreshed.
Seq |
Events |
Controls Initialized |
View State Available |
Form data Available |
What Logic can is written here? |
1 |
Init |
No |
No |
No |
Note: You can access form data etc. by using ASP. Objects but not by Server controls. Creating controls dynamically, in case you had controls to is created on runtime. Any settinginitialization. Master pages and them settings. In this section, we don't have access to ViewState, posted values and neither the controls is initialized. |
2 |
Load View state |
Not guaranteed |
Yes |
Not guaranteed |
You can access view state and any synch logic where you want ViewState to is pushed to behind code variables can is done h Ere. |
3 |
Postbackdata |
Not guaranteed |
Yes |
Yes |
You can access form data. Any logic where you want the form data to is pushed to behind code variables can is done here. |
4 |
Load |
Yes |
Yes |
Yes |
This was the place where you would put any logic want to operate on the controls. Like flourishing a ComboBox from the database, sorting data on a grid, etc. The This event, the we get access to all controls, viewstate and their posted values. |
5 |
Validate |
Yes |
Yes |
Yes |
If your page has validators or you want-to-execute validation for your page, this is the right place to the same. |
6 |
Event |
Yes |
Yes |
Yes |
If This is a post to a button click or a dropdown change and then the relative events would be fired. Any kind of logic which was related to the event can be executed here. |
7 |
Pre-render |
Yes |
Yes |
Yes |
If you want to make final changes to the UI objects like changing tree structure or property values, before these controls is saved in to view state. |
8 |
Save View state |
Yes |
Yes |
Yes |
Once all changes to server controls is done, the this event can be a opportunity to the save control data in to view state. |
9 |
Render |
Yes |
Yes |
Yes |
If you want to add some custom HTML to the output of this are the place you can. |
10 |
Unload |
Yes |
Yes |
Yes |
Any kind of ' would ' like to do here. |
About source code
This is the source code shows the complete ASP. NET request cycle fires. You can download it from here.
References
I am not so smart-to-write this article by myself, lot of things I has plugged from the below articles.
· Read More on IIS 7.0 life cycle http://msdn.microsoft.com/en-us/library/bb470252.aspx
· Intercepting Filters Http://msdn.microsoft.com/en-us/library/ms998536.aspx
· Explains how to implement httphandlers and moduleshttp://msdn.microsoft.com/enus/library/system.web.httpapplication.aspx
· HttpHandlers and httpmodules:-http://www.15seconds.com/Issue/020417.htm
· Implementing security using modules and handlers http://joel.net/articles/asp.net2_security.aspx
· Difference between HttpApplication and global.asaxhttp://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx
License
This article, along with any associated source code and files, is licensed under the Code Project Open License (Cpol)
ASP. NET life cycle (original translation)