学习HttpHandler和HttpModule

一个最最简单的HttpHandler和HttpModule的事例,

首先实现一个HttpHandler类
using  System.Web;
using  System.Collections;

namespace  WhosOn
{
    
public class WhosOnHandler:IHttpHandler
    
{
        
        
public void ProcessRequest(HttpContext objContext)
        
{
            Queue colPageStats;

            colPageStats = ((Queue)objContext.Cache["whoson"]);
            
if (!(colPageStats == null)) 
            
{
                objContext.Response.Write(
"<table border=1 cellpadding=4>");
                objContext.Response.Write(
"<tr><td colspan=4 bgcolor=orange>");
                objContext.Response.Write(
"<b>Who's On</b>");
                objContext.Response.Write(
"</td></tr>");
                objContext.Response.Write(
"<tr colspan=4 bgcolor=#eeeeee>");
                objContext.Response.Write(
"<th>Timestamp</th>");
                objContext.Response.Write(
"<th>Browser Type</th>");
                objContext.Response.Write(
"<th>Remote Address</th>");
                objContext.Response.Write(
"<th>Referrer</th>");
                objContext.Response.Write(
"</td></tr>");
                
                
foreach (StatsEntry objStatsEntry in colPageStats) 
                
{
                    objContext.Response.Write(
"<tr>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.TimeStamp + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.BrowserType + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.UserHostName + "&nbsp;</td>");
                    objContext.Response.Write(
"<td>" + objStatsEntry.Referrer + "&nbsp;</td>");
                }

                objContext.Response.Write(
"</table>");
            }

        }


        
public bool IsReusable 
        
{
            
get 
            
{
                
return true;
            }

        }

    }

}

然后再实现一个HttpModule类
using  System;
using  System.Web;
using  System.Collections;
using  System.Configuration;

namespace  WhosOn
{
    
public class WhosOnModule:IHttpModule
    
{

        
public void Init(HttpApplication myApp)
        
{
            myApp.BeginRequest 
+= new EventHandler(this.OnEnter);
        }


        
public void Dispose()
        
{
        }


        
public void OnEnter(object s, EventArgs e)
        
{
            HttpApplication objApp;
            HttpContext objContext;
            
string strPath;
            Queue colPageStats;
            StatsEntry objStatsEntry;
            
int intMaxEntries;
            objApp 
= ((HttpApplication)s);
            objContext 
= objApp.Context;
            strPath 
= objContext.Request.Path.ToLower();
            if(strPath.EndsWith(".axd"))
            {
                
goto exitMethodDeclaration0;
            }

            intMaxEntries = System.Convert.ToInt32(ConfigurationSettings.AppSettings["whoson"]);
            colPageStats = ((Queue)objContext.Cache["whoson"]);
            
if (colPageStats == null
            
{
                colPageStats 
= new Queue();
            }

            objStatsEntry = new StatsEntry(objContext.Timestamp, objContext.Request.Browser.Type, objContext.Request.UserHostName, objContext.Request.ServerVariables["HTTP_REFERER"]);
            colPageStats.Enqueue(objStatsEntry);

            if (intMaxEntries != 0
            
{
                
while (colPageStats.Count > intMaxEntries) 
                
{
                    colPageStats.Dequeue();
                }

            }

            objContext.Cache["whoson"= colPageStats;
            exitMethodDeclaration0: ;
        }

    }

    
public class StatsEntry
    
{
        
public DateTime TimeStamp;
        
public string BrowserType;
        
public string UserHostName;
        
public string Referrer;

        
public StatsEntry(DateTime TimeStamp, string BrowserType, string UserHostName, string Referrer)
        
{
            
this.TimeStamp = TimeStamp;
            
this.BrowserType = BrowserType;
            
this.UserHostName = UserHostName;
            
this.Referrer = Referrer;
        }

    }

}

将编译的两个WhosOnModule和WhosOnHandler两个dll 放到一个web application的bin目录里,并在这个web application的web.config中添加如下项目
< configuration >
  
< system .web >
    
< httpModules >
    
< add  name ="WhosOnModule"
     type
="WhosOn.WhosOnModule,WhosOnModule"   />
    
</ httpModules >
    
< httpHandlers >
      
< add  verb ="*"  path ="*.axd"
      type
="WhosOn.WhosOnHandler,WhosOnHandler"   />
    
</ httpHandlers >
  
</ system.web >
</ configuration >



你可能感兴趣的:(handler)