您的位置:首页 > 理论基础 > 计算机网络

How to used HttpModule

2006-04-30 17:34 295 查看
HttpModulesallowyoutoextendtheexistingASP.NETpipeline.ThemostcommonuseofHTTPModulesispreand/orepostprecessingofrequests.InternallyASP.NETutilizescustommodules(httpmodules)for:outputcaching,authenticationandauthorizationetc.Therefore,ifyoueverneedtotapintothepiplelinetoaltertherequestinanyway,lookintoHttpModulesastheyprovidearelativelyeasywayforyoutodojustthat(similiartohowISAPIfiltershelpedachievepre/postprocessingcapabilities).

TodevelopanHttpModuleyouarerequiredtodothefollowing:

YouclassmustimplementtheiHttpModuleInterface

BoththeInit(..)andDispose()methodsmustbeinyourclassdefinitions

Belowisasimpleexamplethatoutputs"HttpModulesaysHello!"toeachandeveryrequest.YouwillnoticethatIwiredaneventintheInit(..)methodthatcallsOnBeginRequestatthebeginningofeveryrequest.

usingSystem;
usingSystem.Web;
namespaceCSharpFriends.Samples
{
publicclassCSharpFriendsHttpModule:IHttpModule
{
publicCSharpFriendsHttpModule()
{

[code]}
///
///RequiredbytheinterfaceIHttpModule
///
publicvoidDispose()
{
}
///
///RequiredbytheinterfaceIHttpModule
///IalsowireuptheBeginRequestevent.
///
publicvoidInit(System.Web.HttpApplicationApp)
{
App.BeginRequest+=newSystem.EventHandler(OnBeginRequest);

App.EndRequest+=newSystem.Eventhandler(OnEndRequest);

}
[/code]


publicvoidOnBeginRequest(objectsender,EventArgse)
{
//HttpRequestrequest=((HttpApplication)sender).Request;
HttpApplicationapp=((HttpApplication)sender;
app.Context.Response.Write("HttpModulesaysHello!");
}

publicvoidOnEndRequest(objectsender,EventArgse)
{
HttpApplicationapp=(HttpApplication)sender;
app.Context.Response.Write("HttpModulesaysBaby!");
}


}

}
Inyourweb.config,addthefollowingelements:


<httpModules>

<addtype="CSharpFriends.Samples.CSharpFriendsHttpModule,Test"name="CSharpFriendsHttpModule"/></httpModules>


[/code]


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: