(实战篇)httpmoudle登场亮相

承接上一篇说了httphandler使用的一个小应用,这一篇来看看httpmoudle的一次小亮相,

同样记录下来,以作之后参考。

(1)同样先配置


 <httpModules>
      <add name="OnlineModule" type="CIT.OA.CommonUI.HttpModule.OnlineModule, CIT.OA.CommonUI, Version=1.0.0.0, Culture=neutral"/>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>





(2)配置之后所有的请求都必须经过CIT.OA.CommonUI.HttpModule.OnlineModule这个类处理,而此类究竟做了啥呢?
原来是一个在线统计的实现。
  public class OnlineModule : IHttpModule, IReadOnlySessionState
    {
        /// <summary>
        ///
        /// </summary>
        public OnlineModule()
        {
        }
 
        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(this.context_AcquireRequestState);
        }
 
        private void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            if (CommonFunc.ObjectToNullStr(app.Context.Handler).EndsWith("aspx"))
            {
                string sessionId = FWConfig.CurContext.Session != null ? FWConfig.CurContext.Session.SessionID : String.Empty;
                UserInfo u = FWConfig.CurContext.Session[FWConfig.UserInfoSession] as UserInfo;
 
                if (CommonFunc.IsNotNullString(sessionId) && u != null)
                {
                    OnLineHelper.AddOnLineData(sessionId, u.LoginID);
                }
            }
        }





总结:httpmoudle和httphandler可以做很多事情,这里以最简单的实例来说明,希望可以窥一斑而知全豹。
再次说下它们的区别:前者好比高速公路,只要是车经过(请求),都必须经过它处理,
而后者好比交警,只有固定的请求,如违规,才需要处理。
原文地址:https://www.cnblogs.com/jangwewe/p/2970655.html