会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
xgluxv
路漫漫其修远兮 吾将上下而求索
博客园
首页
新随笔
联系
订阅
管理
公告
学习HttpHandler和HttpModule
Posted on
2004-11-16 00:10
鲁旭
阅读(
1727
) 评论(
2
)
收藏
举报
一个最最简单的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
+
"
</td>
"
);
objContext.Response.Write(
"
<td>
"
+
objStatsEntry.BrowserType
+
"
</td>
"
);
objContext.Response.Write(
"
<td>
"
+
objStatsEntry.UserHostName
+
"
</td>
"
);
objContext.Response.Write(
"
<td>
"
+
objStatsEntry.Referrer
+
"
</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
>
刷新页面
返回顶部
博客园
© 2004-2026
浙公网安备 33010602011771号
浙ICP备2021040463号-3