IHttpModule.BeginRequest firing 2X, Application_BeginRequest firing 1X
asp.net, httpmodule
Solution
This might be too late for the answer but can be useful for someone else. I faced with the same problem. BeginRequest event triggered for twice for each request. I debugged the code and realized that the first trigger for actual resource request but the second is result of "favicon.ico" request. At the beginning of BeginRequest event, a simple check for favicon.ico request eliminates second execution of the method.
public void Application_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
if (ctx.Request.Path == "/favicon.ico") { return; }
Problem
I'm running VS 2008 and .NET 3.5 SP1. I want to implement hit tracking in an `HttpModule` in my ASP.NET app. Pretty simple, I thought. However, the `BeginRequest` event of my `HttpModule` is firing twice for each page hit. The site is very simple right now...no security, just a bit of database work. Should log one row per page hit. Why is this event firing twice? Moreover, `IHttpModule.BeginRequest` actually fires a different number of times for the first page hit when running for the first time (from a closed web browser)...3 times when I'm hitting the DB to provide dynamic data for the page, and only 1 time for pages where the DB isn't hit. It fires 2 times for every page hit after the first one, regardless of whether or not I'm touching the DB. It's interesting to note that `Application_BeginRequest` (in `Global.asax`) is always firing only once. Here's the code: ``` using System; using System.Data; using System.Data.Common; using System.Net; using System.Web; using BluHeron.BusinessLayer; using Microsoft.Practices.EnterpriseLibrary.Data.Sql; namespace BluHeron.HttpModules { public class SiteUsageModule : IHttpModule { public void Init(HttpApplication httpApp) { httpApp.BeginRequest += OnBeginRequest; } static void OnBeginRequest(object sender, EventArgs a) { UsageLogger.LogSiteUsage(((HttpApplication)sender).Context.Request); } public void Dispose() { } } public static class UsageLogger { public static void LogSiteUsage(HttpRequest r) { string ipAddress = GetHostAddress(Dns.GetHostAddresses(Dns.GetHostName())); string browserVersion = r.Browser.Type; string[] urlChunks = r.RawUrl.Split('/'); string page = urlChunks[urlChunks.GetLength(0)-1]; SqlDatabase db = new SqlDatabase(Common.GetConnectionString()); DbCommand cmd = db.GetStoredProcCommand("LogUsage"); db.AddInParameter(cmd, "IPAddress", SqlDbType.NVarChar, ipAddress); db.AddInParameter(cmd, "BrowserVersion", SqlDbType.NVarChar, browserVersion); db.AddInParameter(cmd, "PageName", SqlDbType.NVarChar, page); db.AddInParameter(cmd, "Notes", SqlDbType.NVarChar, ""); db.ExecuteNonQuery(cmd); } private static string GetHostAddress(IPAddress[] addresses) { foreach (IPAddress ip in addresses) { if (ip.ToString().Length <= 15) { return ip.ToString(); } } return ""; } } } ```