asp.net calculates the page execution time by HttpModule

Source: Internet
Author: User
Tags add object copy datetime execution httpcontext net
Create a class library that establishes the following classes:
Copy CodeThe code is as follows:
Using System;
Using System.Collections.Generic;
Using system.web;//references Web namespaces
Using System.Text;
Namespace Timerhttpmodule
{
public class class1:ihttpmodule//Inheritance Ihttpmodules
{
public void init (HttpApplication application)//To 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 beginrequest passed over
Which is stored in the HttpApplication instance.
HttpApplication instance contains 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 build, copy the DLL file to the bin directory, and then register the HttpModule in Web.config:
Copy CodeThe code is as follows:
<configuration>
<system.web>
<add name= "Timerhttpmodule" type= "Timerhttpmodule.class1"/>
</system.web>
</configuration>

This will display the page execution time at the bottom of each. NET page of the site.
Be careful, though, because each. NET page is followed by execution time, including the WebServices and ashx pages, and the. aspx pages that you may not be using to directly do the page (for example, you use to enter JSON data or XML data). Therefore, in order to ensure security, we must also adopt a targeted approach to avoid this situation.
Method One: Before the Response.Write method makes the judgment, excludes some does not want to add the execution time the page, may judge by the Request.url;
Method Two: Do not add execution time directly to the end of the page output, but as an HTTP header output. Use Response.AddHeader (key,value) to achieve this desire.

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.