My first HTTP Module / HTTP Handler example using IIS7 / Vista

Tags: IIS

Here is my first example using HTTP Module and HTTP Handlers with IIS7 in integrated mode.  Note, you can debug your application in ‘integrated mode’, which is really cool!  You’ll need to follow this article to enable debugging.    (This was fixed in SP1) This is one of the powerful things in IIS7.   You can also try out some URL-rewriting tricks.  Here is an article from Scott Guthrie

App_Code/httpmodule1.vb (place in App_Code folder)
———————–

Imports Microsoft.VisualBasic

Public Class TopHTTPModuleNameSpace
    Implements IHttpModule

    Public ReadOnly Property ModuleName() As [String]
        Get
            Return “TopHTTPModuleNameSpace”
        End Get
    End Property

    ‘ In the Init function, register for HttpApplication
    ‘ events by adding your handlers.
    Public Sub Init(ByVal application As HttpApplication) _
            Implements IHttpModule.Init
        AddHandler application.BeginRequest, _
            AddressOf Me.Application_BeginRequest
        AddHandler application.EndRequest, _
            AddressOf Me.Application_EndRequest
    End Sub

    Private Sub Application_BeginRequest(ByVal source As Object, _
            ByVal e As EventArgs)
        ‘ Create HttpApplication and HttpContext objects to access
        ‘ request and response properties.
        Dim application As HttpApplication = CType(source, _
            HttpApplication)
        Dim context As HttpContext = application.Context
        context.Response.Write(“<html>” & vbCrLf)
        context.Response.Write(”    <body>” & vbCrLf & vbCrLf)
    End Sub

    Private Sub Application_EndRequest(ByVal source As Object, _
            ByVal e As EventArgs)
        Dim application As HttpApplication = CType(source, _
            HttpApplication)
        Dim context As HttpContext = application.Context
        context.Response.Write(vbCrLf & vbCrLf & ”    <body>” & vbCrLf)
        context.Response.Write(“</html>” & vbCrLf)
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
End Class


App_Code/httphandler1.vb (place in App_Code folder)
Imports System.Web

Namespace HandlerExample
   
    Public Class MyHttpHandler
        Implements IHttpHandler
       
        ‘ Override the ProcessRequest method.
        Public Sub ProcessRequest(context As HttpContext) _
        Implements IHttpHandler.ProcessRequest
       
            context.Response.Write(“<H1>This is an HttpHandler Test.</H1>”)
            context.Response.Write(“<p>Your Browser:</p>”)
            context.Response.Write(“Type: ” & context.Request.Browser.Type & “<br>”)
            context.Response.Write(“Version: ” & context.Request.Browser.Version & “<br>”)

            If context.Request.Browser.AOL = True Then
                context.Response.Write(“IsAOL: True”)
            Else
                context.Response.Write(“IsAOL: False”)
            End If
        End Sub
       
        ‘ Override the IsReusable property.       
        Public ReadOnly Property IsReusable() As Boolean _
        Implements IHttpHandler.IsReusable
       
            Get
                Return True
            End Get
        End Property
    End Class
End Namespace

Web.config
<?xml version=”1.0″?>
<configuration>
 <appSettings/>
 <connectionStrings/>
 <system.web>
  <authentication mode=”Windows” />
  <compilation debug=”true”/>
 </system.web>
 <system.webServer>
  <modules>
        <add name=”TopHTTPModuleNameSpace” type=”TopHTTPModuleNameSpace”/>
  </modules>
  <handlers>
        <add name=”TheHTTPHandler” verb=”*” path=”Default.aspx” type=”HandlerExample.MyHttpHandler”/>
  </handlers>
 </system.webServer> 
</configuration>

‘Default.aspx test page
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1″ runat=”server”>
<title>My Handler Page Not executing</title>
</head>
 <body>
  <form id=”form1″ runat=”server”>
   <div>
   <% =(Now())%>
   </div>
  </form>
 </body>
</html>

1 Comment

Add a Comment