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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值