- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行 VS 2008 和 .NET 3.5 SP1。
我想在 HttpModule
中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest
我的事件 HttpModule
每次点击页面都会触发两次。该站点现在非常简单......没有安全性,只是一些数据库工作。每页命中应记录一行。为什么这个事件会触发两次?
此外,IHttpModule.BeginRequest
第一次运行时(从关闭的 Web 浏览器),实际上为第一页命中触发了不同的次数......当我点击 DB 为页面提供动态数据时,只有 1 次为未命中数据库的页面。无论我是否在接触数据库,它都会在第一个页面点击后触发 2 次。
有趣的是,Application_BeginRequest
(在 Global.asax
中)总是只触发一次。
这是代码:
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 请求的简单检查消除了该方法的第二次执行。
public void Application_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
HttpContext ctx = app.Context;
if (ctx.Request.Path == "/favicon.ico") { return; }
关于asp.net - IHttpModule.BeginRequest 触发 2X,Application_BeginRequest 触发 1X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2181648/
我创建了一个HttpModule: using System; using System.Web; public class TestModule : IHttpModule { public
我创建了一个HttpModule: using System; using System.Web; public class TestModule : IHttpModule { public
我必须测试有关调用阻止和识别的信息。所以我按照下面的步骤。 (1) 在我的项目中从 [File] > [New] > [Target] 创建一个 Call Directory Extension。 (
我想在 Umbraco 中触发 BeginRequest 事件,但它不起作用。其余代码工作正常。 public class ApplicationEventHandler : IApplication
我已经开始使用New Relic来监控http://alternativeto.net的性能这是一个相当大的网站。 我注意到,他们报告为“TransferRequestHandler”的方法花费了大量
我正在尝试向 .NET Mvc 应用程序中的每个请求添加一些请求上下文信息。我为此目的使用 LogicalCallContext,因为我的请求有时会调用异步任务。但是,我发现在 BeginReques
我有一个 http 模块,但自从迁移应用程序后,.NET 4 一直在经历一些奇怪的行为。 BeginRequest 事件不会在某些请求上触发(但会在其他请求上触发)- 到目前为止,我还没有设法确定何时
我们有一个 Web 应用程序,它运行在 6 个 Web 服务器上,HAProxy 作为负载均衡器。我们使用网络部署在所有网络服务器上同步我们的 IIS 和应用程序。从 1 月开始,一些客户开始报告应用
我正在使用一种有点自制的用户身份验证方法。对用户进行身份验证后,在 C# 中设置身份验证票证。 FormsAuthenticationTicket authenticationTicket = new
我的印象是静态文件(CSS、图像、@font-face 文件等)完全绕过了 ASP.NET,直接由 IIS 提供服务。 但是,每个 HTTP 请求都会调用我的 BeginRequest 事件处理程序,
我正在使用 CA 的 Identity Minder 在现有网站中实现新的 ASP.NET Identity 2.0 框架,该框架主要使用 Request.ServerVariables 来支持所有控
对于来自客户端的给定 HTTP 请求,ASP.NET HttpApplication 的 BeginRequest 和 EndRequest 是否总是发生在完全相同的线程上? 我问的原因是我看到一些非
我正在运行 VS 2008 和 .NET 3.5 SP1。 我想在 HttpModule 中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest我的事件 Htt
我的应用程序中有一些代码需要在每个请求上执行,在执行任何其他操作之前 (甚至在身份验证之前)。到目前为止,我一直在使用 Application_BeginRequest我的 Global.asax 中
我的问题类似于但不完全相同: Why can't my host (softsyshosting.com) support BeginRequest and EndRequest event hand
我们有两个 Web 应用程序(Azure Web 角色),它们在 System.Web.HttpApplication.BeginRequest 期间都会遇到偶尔的长时间延迟(40 到 60 秒)。我
我们有一个 URLRewriting 模块,它在 BeginRequest 事件方法中使用 Response.Redirect 来更改目标页面。 使用 Server.Transfer 或 Server
我是一名优秀的程序员,十分优秀!