As we all know, every Asp.net page is an httphandler, and it is also a control. Let's look at its own definition.
Public class page: templatecontrol, ihttphandler
{
}
When IIS processes HTTP requests, it determines Based on the file extension of the request and finds the corresponding ISAPI.
ASP. NET processes communicate with each other. The Asp.net process initializes a system, such as creating an httpruntime run.
Environment, and so on. many friends in the garden wrote this principle, and the final request will find a handler for execution,
The Asp.net page we usually write is a handler, so it can process requests, and the entry point of this handler is from
Start with the processrequest () method.
The above nonsense is nothing more than trying to say what the processrequest () method has done.
To recognize it.
First, I listed some important methods in the page class:
Public class page: templatecontrol, ihttphandler
{
// Method used to process the returned data
Private void processpostdata (namevaluecollection postdata, bool fbeforeload );
// Page Processing request entry point
Private void processrequest ();
// Processrequestmain () with parameters is called ()
Private void processrequestmain ();
// The core page processing method. Execute a series of familiar methods and events (such as page_init, page_load ....)
Private void processrequestmain (bool implements destagesbeforeasyncpoint, bool implements destagesafterasyncpoint );
}
Let's start with the method at the entrance. Let's take a look at what the processrequest (); method did, and briefly comment it out.
Private void processrequest ()
{
// Set Language and Culture Information
Thread currentthread = thread. currentthread;
Cultureinfo currentculture = currentthread. currentculture;
Cultureinfo currentuiculture = currentthread. currentuiculture;
Try
{
// This is reloaded again.
This. processrequest (True, true );
}
Finally
{
This. restorecultures (currentthread, currentculture, currentuiculture );
}
}
// I simplified the code
Private void processrequest (bool implements destagesbeforeasyncpoint, bool implements destagesafterasyncpoint)
{
If (includestagesbeforeasyncpoint)
{
This. frameworkinitialize (); // framework Initialization
}
Try
{
// This is the focus, and all processing is in this method.
This. processrequestmain (includestagesbeforeasyncpoint, includestagesafterasyncpoint );
}
Catch
{
Throw;
}
}
Finally, let's focus on it. Let's first take a look at the Il of the processrequestmain method and write all the comments in the code, which is basically an event we are familiar with at ordinary times.
// Simplified a lot and removed some code for debugging and tracking.
Private void processrequestmain (bool implements destagesbeforeasyncpoint, bool implements destagesafterasyncpoint)
{
Try
{
Httpcontext context = This. context;
String STR = NULL;
If (includestagesbeforeasyncpoint)
{
// Pre-Initialize and set master pages and application topics
This. initialize mpreinit ();
// Recursive initialization. If the control includes child controls, it is initialized from the inside out to the outside (Child control-> parent control)
This. initrecursive (null );
// Initialization is complete
This. oninitcomplete (eventargs. Empty );
// If it is a resend
If (this. ispostback)
{
// Load View data to controls with view status enabled in sequence. At this stage, we are very familiar with page. loadpagestatefrompersistencemedium ()
// The method will be executed
This. loadallstate ();
// Process the returned data. This method must be executed before and after load. The processpostdata method can be described in many ways. I am going to talk about it in another article.
// Dongdong, when executing before page_load (that is, fbeforeload is true), it mainly involves controls that implement the ipostbackdatahandler interface.
// Process post data and register Post Data Processing events, which are also common loadpostdata ();
// Registerrequiresraiseevent (control. postbackeventhandler); Save the uninitialized controls for the next processdata ..
//... And other operations
This. processpostdata (this. _ requestvaluecollection, true );
}
// The page is finally loaded.
This. onpreload (eventargs. Empty );
// Execute the load event cyclically, and execute the load event of the control cyclically. If the control includes child controls, execute the load event from outside to inside (parent control-> child control ),
// The execution sequence is normal, which is opposite to that of the initrecursive () method. The page_load event is executed here.
This. loadrecursive ();
If (this. ispostback)
{
// The second time the data is processed, the remaining controls that were not processed last time
This. processpostdata (this. _ leftoverpostdata, false );
// This method is mainly used to trigger events that need to be triggered when the data of the control that needs to process the returned data changes, such as the textchanged event of the Textbox Control.
// For example, postbackdatahandler. raisepostdatachangedevent ();
This. raisechangedevents ();
// Cause the control's sending back event. Normally, the control's, such as the button's click event, are executed in this step.
This. raisepostbackevent (this. _ requestvaluecollection );
}
// Page loaded
This. onloadcomplete (eventargs. Empty );
}
// Verify
This. _ request. validaterawurl ();
// Pre-rendering
This. initialize mprerendercomplete ();
// Save the view status, for example, this. savepagestatetopersistencemedium (State );
This. saveallstate ();
This. onsavestatecomplete (eventargs. Empty );
// The last step is rendering.
This. rendercontrol (this. createhtmltextwriter (this. response. Output ));
}
Catch (threadabortexception exception)
{
}
Catch (configurationexception)
{
}
Catch (exception exception3)
{
}
}
This is the execution process of the entire page system from request to output. The code above shows that most of the processing logic of the page is in the processrequestmain () method, this is the execution process of page that I once again read through Il. I hope to share it with you more clearly!
Next, I will analyze how a page handles processpostdata.