IHttpModule.BeginRequest触发2倍,Application_BeginRequest触发1倍 [英] IHttpModule.BeginRequest firing 2X, Application_BeginRequest firing 1X
问题描述
我正在运行VS 2008和.NET 3.5 SP1.
I'm running VS 2008 and .NET 3.5 SP1.
我想在我的ASP.NET应用程序的 HttpModule
中实现点击跟踪.我想很简单.但是,我的 HttpModule
的 BeginRequest
事件对于每次点击都触发两次.该站点现在非常简单...没有安全性,只有一点数据库工作.每页命中应记录一行.为什么此事件触发两次?
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?
此外, IHttpModule.BeginRequest
实际上在首次运行(从封闭的Web浏览器)运行时,针对第一次点击触发了不同的次数...当我点击时,触发了3次DB为该页面提供动态数据,对于未命中该数据库的页面仅提供1次.不论我是否接触数据库,对于第一个命中的页面,它都会触发2次.
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.
有趣的是, Application_BeginRequest
(在 Global.asax
中)始终仅触发一次.
It's interesting to note that Application_BeginRequest
(in Global.asax
) is always firing only once.
代码如下:
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 "";
}
}
}
推荐答案
这可能为时已晚,但对其他人可能有用.我面临着同样的问题.每个请求两次触发BeginRequest事件.我调试了代码,意识到第一个触发实际资源请求,但第二个触发是"favicon.ico"请求的结果.在BeginRequest事件开始时,对favicon.ico请求的简单检查消除了方法的第二次执行.
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; }
这篇关于IHttpModule.BeginRequest触发2倍,Application_BeginRequest触发1倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!