The execution time of the page calculated by httpModule in asp.net

Source: Internet
Author: User

This article introduces how to use httpModule in asp.net to calculate the page execution time. For more information, see.

The default HttpModule in ASP. NET:

DefaultAuthenticationModule ensures that the Authentication object exists in the context. This class cannot be inherited.
FileAuthorizationModule verifies that the remote user has the NT permission to access the requested file. This class cannot be inherited.
FormsAuthenticationModule enables ASP. NET Applications to use Forms authentication. This class cannot be inherited.
The PassportAuthenticationModule provides packaging for the PassportAuthentication service. This class cannot be inherited.
SessionStateModule provides session status services for applications.
UrlAuthorizationModule provides URL-based authorization services to allow or deny access to specified resources. This class cannot be inherited.
WindowsAuthenticationModule enables ASP. NET Applications to use Windows/IIS authentication. Unable to inherit this class


Create a class library and create the following classes:

The Code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Web; // reference the web namespace
Using System. Text;
Namespace TimerHttpModule
{
Public class Class1: IHttpModule // inherits 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;
}
Private void application_EndRequest (object sender, EventArgs e)
{
DateTime endtime = DateTime. Now;
HttpApplication application = (HttpApplication) sender;
HttpContext context = application. Context;
Context. Response. Write ("<p> page execution time:" + (endtime-starttime). ToString () + "</p> ");
}
// The dispose interface must be implemented
Public void Dispose (){}
}
}

After the dll file is generated, copy it to the bin directory and register the HttpModule in web. config:

The Code is as follows: Copy code

<Configuration>
<System. web>
<HttpModules>
<Add name = "TimerHttpModule" type = "TimerHttpModule. Class1"/>
</HttpModules>
</System. web>
</Configuration>

In this way, the execution time of the page is displayed at the bottom of each. net page of the website.

But be careful when doing this, because every. the execution time will be added at the end of the net page, including webservices and ashx pages, and you may not be used to directly create pages. aspx page (for example, json data or xml data ). Therefore, to ensure security, we must also adopt targeted methods to avoid this situation.

Method 1:

Make a judgment before the Response. Write method. to exclude some pages that do not want to add execution time, you can use Request. URL to judge;

Method 2:

Do not add the execution time directly to the end of the page output, but as an http header output. Response. AddHeader (key, value) can be used to achieve this desire.

Summary: an Http request is sent to HttpModule and HttpHandler for processing after it is captured by ASP. NET Framework. Hm and hh are not completely independent. In fact, http requests transfer control to hh in an event during hm transfer, after the real processing is completed in HttpHandler, HttpHandler will return the control to HttpModule again.
In the above Code, the Init () parameter of HttpModule is of the HttpApplication type. It has many events, including BeginRequest, EndRequest, AuthentiacteRequest, etc.

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.