共用方式為


Example HTTP Module 

This code example demonstrates how to implement an HTTP module. For details about how to use the example in this topic and links to the other files in this example, see How to: Create and Configure an HTTP Module.

Example

The following code example illustrates a complete HTTP module that is written as a standalone compiled assembly named Samples.Aspnet.HttpModuleExamples. If you compile the assembly, the DLL file must exist in the Bin folder of the same ASP.NET application as the Web.config file that lists it, or you can add it to the Global Assembly Cache.

Alternatively, you can store the source-code file in the App_Code directory of the ASP.NET application. For an example that uses this kind of implementation, see How to: Create Custom HTTP Modules. For information about compilation options, see ASP.NET Compilation Model.

After you add this HTTP module to an ASP.NET application and configure it manually or using the code in the Example Configuration Code for an HTTP Module, any request to ASP.NET content in the Web site will display the following message from the module:

Current user: <user name>

Time span between begin-request and end-request events: <timespan>

Your friendly HttpModule: RequestTimeIntervalModule

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.Security.Principal

Namespace Samples.Aspnet.HttpModuleExamples

    Public Class RequestTimeIntervalModule
        Implements IHttpModule

        Public Sub Dispose() Implements IHttpModule.Dispose
        End Sub

        ' Add event handlers to the HttpApplication.
        Public Sub Init(ByVal httpApp As HttpApplication) Implements IHttpModule.Init
            AddHandler httpApp.BeginRequest, AddressOf OnBeginRequest
            AddHandler httpApp.EndRequest, AddressOf OnEndRequest
        End Sub

        ' Record the time of the begin request event.
        Public Sub OnBeginRequest(ByVal sender As [Object], ByVal e As EventArgs)
            Dim httpApp As HttpApplication = sender
            httpApp.Context.Items("beginRequestTime") = DateTime.Now
        End Sub

        Public Sub OnEndRequest(ByVal sender As [Object], ByVal e As EventArgs)
            Dim httpApp As HttpApplication = sender
            Dim beginRequestTime As DateTime = _
                CType(httpApp.Context.Items("beginRequestTime"), DateTime)
            ' Evaluate the time between the begin and the end request events.
            Dim ts As TimeSpan = DateTime.Now - beginRequestTime
            ' Write the time span out as a response header.
            httpApp.Context.Response.AppendHeader("TimeSpan", ts.ToString())
            ' Get the current user's ID.
            Dim user As String = WindowsIdentity.GetCurrent().Name

            ' Display the information in the page.
            Dim sb As New StringBuilder
            sb.AppendLine("<H2>RequestTimeInterval HTTP Module Output</H2>")
            sb.AppendLine("Current user: ")
            sb.AppendLine(user)
            sb.AppendLine("<br/>Time span between begin-request and end-request events: ")
            sb.AppendLine(ts.ToString())

            httpApp.Context.Response.Output.WriteLine(sb.ToString)
        End Sub
    End Class

End Namespace
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Security.Principal;

namespace Samples.Aspnet.HttpModuleExamples
{
    public class RequestTimeIntervalModule : IHttpModule
    {
        public void Dispose()
        {
        }

        // Add event handlers to the HttpApplication.
        public void Init(HttpApplication httpApp)
        {
            httpApp.BeginRequest += new EventHandler(OnBeginRequest);
            httpApp.EndRequest += new EventHandler(OnEndRequest);
        }

        // Record the time of the begin request event.
        public void OnBeginRequest(Object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;
            httpApp.Context.Items["beginRequestTime"] = DateTime.Now;
        }

        public void OnEndRequest(Object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;
            
            // Get the time of the begin request event.
            DateTime beginRequestTime =
                (DateTime)httpApp.Context.Items["beginRequestTime"];
            
            // Evaluate the time between the begin and the end request events.
            TimeSpan ts = DateTime.Now - beginRequestTime;
            
            // Write the time span out as a request header.
            httpApp.Context.Response.AppendHeader("TimeSpan", ts.ToString());
            
            // Get the current user's ID.
            string user = WindowsIdentity.GetCurrent().Name;

            // Display the information in the page.
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<H2>RequestTimeInterval HTTP Module Output</H2>");
            sb.AppendFormat("Current user: {0}<br/>", user);
            sb.AppendFormat("Time span between begin-request and end-request events: {0}<br/>", 
                ts.ToString());

            httpApp.Context.Response.Output.WriteLine(sb.ToString());

        }

    }
}

See Also

Tasks

How to: Create and Configure an HTTP Module

Reference

httpModules Element (ASP.NET Settings Schema)
HttpModuleAction
HttpModuleActionCollection
HttpModulesSection

Concepts

Introduction to HTTP Modules
Example Configuration Code for an HTTP Module