My first HttpModule - written in Whidbey

I just wrote my first HttpModule.  Admittedly, it doesn't do much, but I know that it works.  All it does is get the IP host address of the remote client and put that into the Event Log.  Admittedly, the Module does not do a lot, but it gets me started.  The web.config of an asp.net application needs to have the entry below put into the httpModules section.  The code for the module is below that.

Web.Config:

   <httpModules>

      <add type="HttpTestModule.cTestModule, HttpTestModule" name="HttpModuleTest"/>

   </httpModules>

           

Code:

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

 

namespace HttpTestModule

{

      public class cTestModule : IHttpModule

      {

            public void Init(System.Web.HttpApplication app)

            {

                  app.BeginRequest += new EventHandler(app_BeginRequest);

                  app.EndRequest += new EventHandler(app_EndRequest);

            }

 

            void app_EndRequest(object sender, EventArgs e)

            {

            }

 

            void app_BeginRequest(object sender, EventArgs e)

            {

                  HttpApplication app = (HttpApplication)sender;

                  string strAddress = app.Request.UserHostAddress;

                  System.Diagnostics.EventLog.WriteEntry("HttpModule", "IpAddress: " + strAddress);

            }

 

            void app_CompleteRequest(object sender, EventArgs e)

            {

            }

           

            public void Dispose()

            {

            }

      }

}

 

No Comments