記錄用戶訪問頁面的記錄。

注意一定要加

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

 
    }

發佈了157 篇原創文章 · 獲贊 3 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章