In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to treat the ASP.NET application and page life cycle, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
We will learn about the different events, and the life cycle of an ASP.NET application starts with the browser sending a request to the Web server (usually IIS for ASP.NET applications) until the result of the request is returned to the browser. In the process, we first need to understand the two rough steps of the ASP.NET request. Secondly, we will take a detailed look at the execution order and logic of different events in 'httphandler', 'httpmodule' and asp.net page objects (Page).
A two-step process:
Asp.net request processing, the 2-step process is as follows, and the user sends a request to the IIS server:
1. Asp.net creates a runtime that can process requests. In other words, it creates application objects, requests, responses, and context objects to handle requests.
2. Once the runtime is created, the request is processed through a series of event handling modules, Handler processing and page objects. Referred to as MHPM (Module, handler, page and Module event).
The various stages of the ASP.NET application life cycle:
Step 1: the user requests application resources from the Web server the life cycle of the ASP.NET application starts with the browser sending a request to the Web server (usually IIS for ASP.NET applications). ASP.NET is an ISAPI extension under the Web server. When the Web server receives a request, it checks the file extension of the requested file to determine which ISAPI extension should process the request, and then passes the request to the appropriate ISAPI extension. ASP.NET handles file extensions that have been mapped to them, such as .aspx, .ascx, .ashx, and .asmx.
Step 2:ASP.NET receives * requests for an application when ASP.NET receives * requests for any resource in the application, a class named ApplicationManager creates an application domain. The application domain provides application isolation for global variables and allows each application to be unloaded individually. In the application domain, an instance of a class named HostingEnvironment is created that provides access to information about the application, such as the name of the folder where the application is stored.
Step 3: after the application domain is created and the HostingEnvironment object is instantiated for each request, ASP.NET creates and initializes the core objects, such as HttpContext, HttpRequest, and HttpResponse. The HttpContext class contains objects specific to the current application request, such as HttpRequest and HttpResponse objects. The HttpRequest object contains information about the current request, including Cookie and browser information. The HttpResponse object contains the response sent to the client, including all rendered output and Cookie.
Step 4: after assigning the HttpApplication object to the request to initialize all core application objects, the application is launched by creating an instance of the HttpApplication class. If the application has a Global.asax file, ASP.NET creates an instance of the Global.asax class (derived from the HttpApplication class) and uses that derived class to represent the application.
Note: * * each time an ASP.NET page or process is requested in an application, a new instance of HttpApplication will be created. However, to maximize performance, HttpApplication instances can be reused for multiple requests.
Step 5: request MHPM (Module, handler, page and Module event) is processed by the HttpApplication pipeline.
Use MHPM (Module, handler, page and Module event) events to process requests
Once the HttpApplication object is created, it will be executed by the HttpApplication class when the request is processed. Let's take a look at HttpModule HttpHandlers.
1. If you want to handle program logic through * .aspx,*.html files, then you need to use HttpHandler, in other words, httphandler is an extended processor.
2. If you want to use the ASP.NET pipeline to handle program logic, you need to use HttpModule in other words, httpmodule is an event handler.
How to handle the request. Mhpm has four important steps:
Step 1 (M: HttpModule): the client starts request processing. Events in the asp.net engine and httpmodule can be used to handle user-defined logic. There are 6 important events that you can call Begin Request,authenticaterequest,authorizerequest,resolverequestcache,acquirerequeststate and prerequesthandlerexecute before your page object is created.
Step 2 (H: 'HttpHandler'): once the above six events have been executed, the asp.net engine calls the processrequest event if httphandler has been implemented in your project.
Step 3 (P: ASP.NET page): once the httphandler is executed, the asp.net page object is created. While the asp.net page object is created, the event of the Page object will be called, which can help us to customize the handling of the custom logic on the page. There are six important events, Init, Load, validate, event, render, unload for short SILVER S-Start (does not signify anything as such just forms the word), I-(Init), L (Load), V (Validate), E (Event) and R (Render).
Step 4 (M: HttpModule): once the page object is executed and unloaded from memory, httpmodule provides page execution events that can be used to inject custom processing logic. There are four important post-processing events postrequesthandlerexecute,releaserequeststate,updaterequestcache and endrequest.
Detailed description of the event:
Part of the Event event Description describes that the HttpModuleBeginRequest occurs as * * events in the HTTP execution pipeline chain when the ASP.NET responds to the request. The BeginRequest event signals the creation of any given new request. This event is always raised and is always a * * event that occurs during request processing. HttpModuleAuthenticateRequest occurs when the security module has established a user identity. The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request. Occurs when the HttpModuleAuthorizeRequest security module has verified user authorization. The AuthorizeRequest event signals that ASP.NET has authorized the current request. HttpModuleResolveRequestCache occurs after the ASP.NET completes the authorization event to cause the cache module to service the request from the cache, bypassing the execution of an event handler, such as a page or XML Web services. HttpModuleAcquireRequestState occurs when ASP.NET gets the current state (such as session state) associated with the current request. The AcquireRequestState event is raised after the event handler is created. HttpModulePreRequestHandlerExecute occurs just before ASP.NET starts executing event handlers (for example, a page or a XML Web services). The HttpHandlerProcessRequestHttphandler logic is executed. In this section, we will write about the logic that needs to be extended for each page. PageInit
The OnInit method performs the initialization and setup steps required to create an instance of Page. During this phase of the page life cycle, the server controls declared in the page have been initialized to the default state; however, the view state of each control has not been populated. During the Page_Init phase, controls on a page cannot access other server controls on the page, whether the other controls are child controls or parent controls. Other server controls may not be created or accessed.
The PageLoadASP.NET control finishes loading, and you write the UI operation logic or any other logic here. PageValidate if you have verification logic on your page, check here to see if it conforms to the verification. Render sends the final output of the page to the browser, and if you want to make some final HTML changes, this is output to the browser, where you can enter the logic of your HTML. The PageUnload page object is unloaded from memory. HttpModulePostRequestHandlerExecute occurs when an ASP.NET event handler (for example, a page or a XML Web service) finishes execution. HttpModuleReleaserequestState occurs after the ASP.NET has executed all request event handlers. This event causes the state module to save the current state data. After the ReleaseRequestState event is raised, the application ends with the request and sends an ASP.NET signal to store the request state. HttpModuleUpdateRequestCacheBefore you end, if you want to update your cache. Before you finish, if you want to update your cache. HttpModuleEndRequest occurs when the ASP.NET finishes executing the event handler so that the cache module stores the response that will be used to service subsequent requests from the cache.
Demo code:
In this code, we create HttpModule and Httphandler to add all the events that request the response. Let's use HttpModule Httphandler to track all activities and add it to a global collection variable.
Public class clsHttpModule: IHttpModule {private HttpApplication httpApp; public static ArrayList objArrayList = new ArrayList (); public clsHttpModule () {} public void Dispose () {} public void Init (HttpApplication context) {this.httpApp = context HttpApp.Context.Response.Clear (); objArrayList.Clear (); objArrayList.Add ("httpModule:Init"); httpApp.AuthenticateRequest + = new EventHandler (OnAuthentication); httpApp.AuthorizeRequest + = new EventHandler (OnAuthorization); httpApp.BeginRequest + = new EventHandler (OnBeginrequest); httpApp.EndRequest + = new EventHandler (OnEndRequest) HttpApp.ResolveRequestCache + = new EventHandler (OnResolveRequestCache); httpApp.AcquireRequestState + = new EventHandler (OnAcquireRequestState); httpApp.PreRequestHandlerExecute + = new EventHandler (OnPreRequestHandlerExecute); httpApp.PostRequestHandlerExecute + = new EventHandler (OnPostRequestHandlerExecute); httpApp.ReleaseRequestState + = new EventHandler (OnReleaseRequestState); httpApp.UpdateRequestCache + = new EventHandler (OnUpdateRequestCache) } 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 strin objArrayList) {httpApp.Context.Response.Write (str + ")
");}
The following code snippet is a ProcessRequest 'event' that tracks HttpHandler:
Public class clsHttpHandler: IHttpHandler {public bool IsReusable {get {return true;} public void ProcessRequest (HttpContext context) {clsHttpModule.objArrayList.Add ("HttpHandler:ProcessRequest"); context.Response.Redirect ("~ / Default.aspx");}}
Page Page event
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");}}
Configuration in webconfig:
Page loading effect * times:
After clicking the button button:
HttpModule
HttpModule inserts itself into the ASP.NET request processing pipeline by registering in certain events. When these events occur, the ASP.NET calls the corresponding HTTP module so that the module can process the request.
Common actions:
1. Dynamically add some notes or illustrative text to each page:
2. Judge user login
If more than one HttpModule is defined, the order in which the custom HttpModule is introduced into the web.config file determines the order in which the multiple custom HttpModule takes over when processing a HTTP request.
HttpHandler
HttpHandler is the processing center of HTTP request, which really compiles and executes the server page requested by the client, and appends the processed information to the HTTP request information flow and returns it to HttpModule again.
HttpHandler is different from HttpModule in that once its own HttpHandler class is defined, its relationship to the HttpHandler of the system will be "overridden".
To verify whether the request has entered the HttpHandler, we can add the following to the WebConfig:
Have a look at the debugger.
To sum up, we can sum up a picture:
ASP.NET page event
In the above section, we have seen a complete request event for the ASP.NET page. One of the most important parts is the ASP.NET page, which we did not discuss in detail. Let's discuss the page event of ASP.NET in detail. There are two sections in any ASP.NET page as shown in the figure:
Note: most developers directly use everything in the page_load method, which is not a good practice. Such as populating controls, setting view state, applying themes, and so on, these are the loads that occur on the page. Therefore, we can add the appropriate logic to the appropriate events, which will really make your code clean and logical.
Whether the sequence number event control initializes the state
Whether the available form data
But what logic can be written here? 1InitNoNoNo
The OnInit method performs the initialization and setup steps required to create an instance of Page. During this phase of the page life cycle, the server controls declared in the page have been initialized to the default state; however, the view state of each control has not been populated. During the Page_Init phase, controls on a page cannot access other server controls on the page, whether the other controls are child controls or parent controls. It is not necessary to create other server controls or to access them.
2Load view stateNot guaranteedYesNot guaranteed you can access view state and any synchronization logic 3PostBackdataNot guaranteedYesYes you can access form data. 4LoadYesYesYes bound controls and so on. 5ValidateYesYesYes if your page has authentication, or you want to perform validation for your page. 6EventYesYesYes if this is a change by clicking a button or drop-down, then the corresponding event will be executed. 7Pre-renderYesYesYes if you want to modify the structure or property values of the UI object before the view state of these controls is saved. 8Save view stateYesYesYes once all server controls have been changed, this event controls the data to be saved in view state. 9RenderYesYesYes if you want to add some custom HTML output. 10UnloadYesYesYes
You can do any cleaning here.
After reading the above, have you mastered how to view the life cycle of ASP.NET applications and pages? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.