httpmodule是如何工作的
当一个http请求到达httpmodule时,整个asp教程.net framework系统还并没有对这个http请求做任何处理,也就是说此时对于http请求来讲,httpmodule是一个http请求的“必经之路”,所以可以在这个http请求传递到真正的请求处理中心(httphandler)之前附加一些需要的信息在这个http请求信息之上,或者针对截获的这个http请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的http请求,从而可以起到一个filter过滤器的作用
using system;
using system.collections.generic;
using system.text;
using system.web;
namespace myhttpmodule
{
///
/// 说明:用来实现自己的httpmodule类。
///
public class myfirsthttpmodule : ihttpmodule
{
private void application_beginrequest(object sender, eventargs e)
{
httpapplication application = (httpapplication) sender;httpcontext context = application.context;
httprequest request = application.request;
httpresponse response = application.response;response.write("我来自自定义httpmodule中的beginrequest
");
}
private void application_endrequest(object sender, eventargs e)
{
httpapplication application = (httpapplication) sender;httpcontext context = application.context;
httprequest request = application.request;
httpresponse response = application.response;response.write("我来自自定义httpmodule中的endrequest
");
}#region ihttpmodule 成员
public void dispose()
{
}
public void init(httpapplication application)
{
application.beginrequest += new eventhandler(application_beginrequest);
application.endrequest += new eventhandler(application_endrequest);
}#endregion
}
}
httpmodule 的权限管理
////下面是web.config 里面的
///这样每执行个页面会调用这个方法。
.net代码
using system.configuration;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;///
///class1 的摘要说明
namespace mymodule
{
public class mymodule : ihttpmodule
{
///
/// 初始化加入两个事件到application 里面去.
///
///
public void init(httpapplication application)
{
application.beginrequest += (new
eventhandler(this.application_beginrequest));
application.endrequest += (new
eventhandler(this.application_endrequest));
}
///在这里必须相当于是把response转过来。在这里根本直接用不了。不知道为啥。
///写这个的作用是进行权限判断。
private void application_beginrequest(object source, eventargs e)
{
httpapplication application = (httpapplication)source;
httpresponse responseo教程k = application.context.response;
responseok.write("beginning of request
");
}
private void application_endrequest(object source, eventargs e)
{
httpapplication application = (httpapplication)source;
httpresponse responseok = application.context.response;
responseok.write("end of request
");
}
public void dispose() { }
}
}