注意一定要加
context.BeginRequest += new EventHandler(context_BeginRequest);
因为它的 init 事件只会执行一次。而 BeginRequest 事件则每次请求都会执行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RecordHandler : IHttpModule
{
private void InsertRecord(HttpApplication context)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
DataAccess.SqlProvider db = new DataAccess.SqlProvider(connString);
string pageName = context.Context.Request.FilePath;
string sqlText = string.Format("insert into tbpageRecord(page) values('{0}')", pageName);
db.ExecuteSQL(sqlText);
db.disCounect();
}
#region IHttpModule 成员
void IHttpModule.Dispose()
{
throw new NotImplementedException();
}
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication context = (HttpApplication)sender;
InsertRecord(context);
}
#endregion
}