IHttpModule

随便写一个类继承IHttpModule

实现IHttpModule中的两个方法 

Init()

Dispose()

        public void Init(HttpApplication context)
        {
            //throw new NotImplementedException();
            //恰好在 ASP.NET 开始执行事件处理程序(例如,某页或某个 XML Web services)前发生。
            context.PreRequestHandlerExecute += new EventHandler(Context_PreRequestHandlerExecute);
            //在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生。
            context.PostRequestHandlerExecute += new EventHandler(Context_PostRequestHandlerExecute);

        }
        //在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生。
        //页面加载完成后触发
        private void Context_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
        //恰好在 ASP.NET 开始执行事件处理程序(例如,某页或某个 XML Web services)前发生。
        //页面加载前触发
        private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //获取到当前请求上下文
            HttpContext context = ((HttpApplication)sender).Context;
            var request = context.Request;
            //浏览器
            string browser = request.Browser.Browser;
            if (string.IsNullOrEmpty(browser))
            {
                browser = "/home/index";
            }
            //绝对路径
            string url = request.Url.AbsolutePath;
            string host = request.Url.Host;





        }

然后还需要到配置文件中配置一下

在<system.webServer>这个节点下

    <modules runAllManagedModulesForAllRequests="true" >
      <add name="HttpModule111" type="MVCWebCount.Common.HttpModule111" />
    </modules>

或者

    <modules runAllManagedModulesForAllRequests="true" >
      <add name="HttpModule111" type="MVCWebCount.Common.HttpModule111,MVCWebCount" />
    </modules>

name就是自己随便写的那个类

type是这个类所在的地址,后半截是他所在的程序集

原文地址:https://www.cnblogs.com/ansheng/p/5486474.html