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());
}
}
}