web developer tips (43):通过请求处理管道注册IHttpModule
2009-09-07 00:58
405 查看
原文地址:The way you register your IHttpModule depends on the request processing pipeline in which your module will run
建立和运行自定义IHttpModule需要做三件简单的事:
1、把源码编译成dll。
2、在net里注册。
3、在IIS配置系统里注册。
http://www.watch-life.net/visual-studio/register-your-ihttpmodule-depends-on-the-pipeline.html
1)假定你有个简单的模块,在验证发生之前,触发一个用HttpContext 做参数的事件,在BeginRequest事件写如下代码:
public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(context_BeginRequest);
}
public void Dispose() { }
public class PreAuthEventArgs : EventArgs
{
public HttpContext httpContext;
public PreAuthEventArgs(HttpContext context)
{
httpContext = context;
}
}
public delegate void PreAuthRequestEventHandler(object sender, PreAuthEventArgs e);
public event PreAuthRequestEventHandler PreAuthEvent;
void context_BeginRequest(object sender, System.EventArgs e)
{
if (PreAuthEvent != null)
{
PreAuthEvent(this, new PreAuthEventArgs(HttpContext.Current));
}
}
}
把模块编译成dll文件,在Visual Studio里右键单击项目,选择属性,在“应用”标签页的输出项选择“类库”
2)在网站里,有两个方法注册dll:
A)用GAC注册。为此你需要要有管理员权限。使用Global Assembly Cache Tool(Gacutil.exe)添加程序集到GAC:
gacutil /if YouModule.dll
你可以在项目编译的任何时候来注册GAC,在项目属性里的“生成事件”里可以完成程序集的GAC注册,在“生成后事件执行命令行”里输入:
B)然而,最常用的解决方法是,在网站的bin目录替换dll,通过这个方法不会打破通过xcopy部署网站的方式。
3)最后,需要把模块注册到IIS的配置系统里。整个这篇文章的目的就是说明,当网站在执行的中的时候,通过管道(pipeline )注册模块有什么不同。
IIS7的推出了集成模式的概念,要求集成处理使用IIS和ASP.Net请求处理管道。在IIS6中,IIS和ASP.NET请求管道是独立的,在在IIS7中是向后兼容的经典模式。如果你的网站或应用程序是执行的经典模式,或在IIS6的服务器运行,则需要注册不同的模块。
IIS6和IIS7的经典模式,在web.config里注册模块 ,代码如下:
在IIS7的集成模式,web.config代码如下:
有关更多注册HTTP 模块的详细内容见:Creating and Registering a Custom HTTP Module
更多文章见:守望轩[http://www.watch-life.net/]
建立和运行自定义IHttpModule需要做三件简单的事:
1、把源码编译成dll。
2、在net里注册。
3、在IIS配置系统里注册。
http://www.watch-life.net/visual-studio/register-your-ihttpmodule-depends-on-the-pipeline.html
1)假定你有个简单的模块,在验证发生之前,触发一个用HttpContext 做参数的事件,在BeginRequest事件写如下代码:
public void Init(HttpApplication context)
{
context.BeginRequest += new System.EventHandler(context_BeginRequest);
}
public void Dispose() { }
public class PreAuthEventArgs : EventArgs
{
public HttpContext httpContext;
public PreAuthEventArgs(HttpContext context)
{
httpContext = context;
}
}
public delegate void PreAuthRequestEventHandler(object sender, PreAuthEventArgs e);
public event PreAuthRequestEventHandler PreAuthEvent;
void context_BeginRequest(object sender, System.EventArgs e)
{
if (PreAuthEvent != null)
{
PreAuthEvent(this, new PreAuthEventArgs(HttpContext.Current));
}
}
}
把模块编译成dll文件,在Visual Studio里右键单击项目,选择属性,在“应用”标签页的输出项选择“类库”
2)在网站里,有两个方法注册dll:
A)用GAC注册。为此你需要要有管理员权限。使用Global Assembly Cache Tool(Gacutil.exe)添加程序集到GAC:
gacutil /if YouModule.dll
你可以在项目编译的任何时候来注册GAC,在项目属性里的“生成事件”里可以完成程序集的GAC注册,在“生成后事件执行命令行”里输入:
/if $(TargetPath)
B)然而,最常用的解决方法是,在网站的bin目录替换dll,通过这个方法不会打破通过xcopy部署网站的方式。
3)最后,需要把模块注册到IIS的配置系统里。整个这篇文章的目的就是说明,当网站在执行的中的时候,通过管道(pipeline )注册模块有什么不同。
IIS7的推出了集成模式的概念,要求集成处理使用IIS和ASP.Net请求处理管道。在IIS6中,IIS和ASP.NET请求管道是独立的,在在IIS7中是向后兼容的经典模式。如果你的网站或应用程序是执行的经典模式,或在IIS6的服务器运行,则需要注册不同的模块。
IIS6和IIS7的经典模式,在web.config里注册模块 ,代码如下:
<configuration> <system.web> <httpModules> <add name="AuthenticationCheckerModule " type="AuthenticationCheckerModule "/> httpModules> system.web> configuration>
在IIS7的集成模式,web.config代码如下:
<configuration> <system.webServer> <modules> <add name="AuthenticationCheckerModule " type="AuthenticationCheckerModule "/> modules> system.webServer> configuration>
有关更多注册HTTP 模块的详细内容见:Creating and Registering a Custom HTTP Module
更多文章见:守望轩[http://www.watch-life.net/]
相关文章推荐
- web developer tips (43):通过请求处理管道注册IHttpModule
- web developer tips (43):通过请求处理管道注册IHttpModule
- 学习ASP.NET Core,你必须知道“中间件”是什么?中间件如何注册?请求处理管道是如何通过中间件构建的?
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[中]:管道如何处理请求
- 要处理此请求,必须在配置中注册 WebResource.axd 处理程序
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[下]:管道是如何构建起来的?
- 学习ASP.NET Core, 怎能不了解请求处理管道[5]: 中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[上]:采用管道处理请求
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- 基于管道化和事件驱动模型的Web请求处理(二)
- 46. Spring Boot中使用AOP统一处理Web请求日志
- 学习ASP.NET Core, 怎能不了解请求处理管道[6]: 管道是如何随着WebHost的开启被构建出来的?
- 引用开源框架通过AsyncHttpClient处理get/post请求
- mvc 请求处理管道
- 读Spring的源代码二:DispatcherServlet处理web请求的过程
- 基于JavaScript、Javabean、Servlet、ajax的异步请求登录注册找回密码Javaweb项目
- 无法在web服务器上启动调试。服务器因语法无效而未能处理调试请求。
- 甩掉ashx和asmx使用jQuery.ajaxWebService请求WebMethod简练处理Ajax
- Spring Web MVC处理请求流程
- 处理“System.Web.HttpException: 超过了最大请求长度”