I. Understand the operating mechanism of ASP. NET (for example, using httpmodule to calculate the page execution time)

Source: Internet
Author: User

I. Brief introduction to the implementation steps of Asp.net

1. IIS receives customer requests

2. IIS sends the request to aspnet_isapi.dll for processing.

3. (if this is the first timeProgram) Load the DLL in the bin directory.

4. (If the program is run for the first time) read the configurations in webconfig at all levels

5. (If the program is run for the first time) Compile and load global. asax and initialize the httpapplication instance.

6. Create httpcontext in response to the request

7. Create an httptextwriter that carries the response results

8. Find the appropriate httphandler (Asp.net page) and process the HTTP request

9. Process session, exception

10. Feedback the processing results to customers

After an httpapplication instance is created,
The initmodules () method is called,
This method calls the corresponding httpmodule according to the configuration in the webconfig file.
This is the customizable httpmodule.

2. Customize httpmodule to calculate the page execution time

When httpapplication creates an httpmodule
The init method of httpmodule will be executed.
In this method, you can subscribe to multiple events.
As follows:
Beginrequest
Authenticaterequest occurs when the security module has a user ID.
Postauthenticaterequest
Authorizerequest occurs when the security module has verified user authorization.
Postauthorizerequest
Resolverequestcache
Postresolverequestcache
Postmaprequesthandler
Acquirerequeststate
Postacquirerequeststate
Prerequesthandlerexecute
Postrequesthandlerexecute
Releaserequeststate
Postreleaserequeststate
Endrequest
These events are also handled by httpapplication in various events in the pipeline.
Commonly used are beginrequest and endrequest.

The following is an example to calculate the page execution time.
First look at the webconfigCode

 

 

<? Xmlversionxmlversionxmlversionxmlversion = "1.0" ?>
< Configuration >
< System. Web >
< Httpmodules >
< Add Name = "Mymodule" Type = "Xland. mymodule" />
</ Httpmodules >
</ System. Web >
</ Configuration >  

Xland is a class library created by me. The namespace is xland.
Mymodule implements the ihttpmodules interface for a class in this class library.
The code for this class is as follows:

 

 

Using System;
Using System. Collections. Generic;
Using System. Web; // Reference a web namespace
Using System. text;
Namespace Xland
{
Public   Class Mymodule: ihttpmodule // Inherit ihttpmodules
{

Public   Void Init (httpapplication Application) // Implement the init event in ihttpmodules
{
// Subscribe to two events
Application. beginrequest + = New Eventhandler (application_beginrequest );
Application. endrequest + = New Eventhandler (application_endrequest );
}
Private Datetime starttime;
Private   Void Application_beginrequest ( Object Sender, eventargs E)
{
// Object sender is the object passed by beginrequest.
// It stores the httpapplication instance.
// The httpapplication instance contains the httpcontext attribute.
Starttime = Datetime. now;
Httpapplication = (Httpapplication) sender;
Httpcontext Context = Application. context;
Context. response. Write ( " Start timing. The current time is: "   + Starttime. tostring ( " Yyyy-mm-dd hh: mm: Ss. fff " ) +   " <Br> " );
}
Private   Void Application_endrequest ( Object Sender, eventargs E)
{
Datetime endtime = Datetime. now;
Httpapplication = (Httpapplication) sender;
Httpcontext Context = Application. context;
Context. response. Write ( " Timing ends. The current time is: "   + Endtime. tostring ( " Yyyy-mm-dd hh: mm: Ss. fff " ) +   " <Br> " );
Context. response. Write ( " Page execution time: "   + (Endtime - Starttime). tostring ());
}
// Must implement the dispose Interface
Public   Void Dispose (){}
}
}

 

Well, you don't need to worry about anything else. We do some time-consuming operations in default. aspx.

Note that xland references must be added to the web application class library.

Using System;
Using System. collections;
Using System. configuration;
Using System. DaTa;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Namespace _ 1
{
Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
For ( Int I =   1 ; I <   10000 ; I ++ )
{
Response. Write (I );
If (I %   100   =   0 ) {Response. Write ( " <Br> " );}
}
}
}
} You can do many things in beginrequest and endrequest.

For example, the domain used to process cookies.
You must be able to come here when you need it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.