惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Vercel News
Vercel News
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
Jina AI
Jina AI
B
Blog
Recorded Future
Recorded Future
MyScale Blog
MyScale Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
爱范儿
爱范儿
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Schneier on Security
Schneier on Security
V
V2EX
TaoSecurity Blog
TaoSecurity Blog
H
Hacker News: Front Page
Cloudbric
Cloudbric
D
DataBreaches.Net
B
Blog RSS Feed
P
Palo Alto Networks Blog
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
I
Intezer
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
K
Kaspersky official blog
Forbes - Security
Forbes - Security

博客园 - 永春

随笔 随笔 asp+access在64位上的问题 换个活法 项目做的很无语 五分钟叫你看懂美国金融危机的成因和巨大危害[转] Sqlserver中的日期类型值不能小于1753年 C#读取WORD时发生“拒绝访问”及“消息筛选器显示应用程序正在使用中”异常的处理 Asp.Net之自定义表达式构造器(ExpressionBuilder) [Sharepoint2007对象模型]第三回:Web应用程序(SPWebApplication) Sharepoint2007对象模型系列 [Sharepoint2007对象模型]第二回:Web应用程序服务(SPWebService) [Sharepoint2007对象模型]第一回:服务器场(SPFarm) C#强化系列文章九:代码访问安全性使用 项目经理的个人修养 《博客园精华集--Sharepoint分册》第三轮结果 《博客园精华集》Sharepoint+MOSS分册第2轮筛选结果文章列表 Asp.Net页面执行流程分析 在MOSS中使用.Net3.5中的Ajax功能
C#强化系列文章八:HttpModule,HttpHandler,HttpHandlerFactory简单使用
永春 · 2008-05-07 · via 博客园 - 永春

这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定。本文通过一个简单的例子来直观的比较一下这三个对象的使用。
HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序
HttpHandler:Http处理程序,处理页面请求
HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序

例子很简单,就是在每个页面的头部加入一个版权声明。
一、HttpModule
这个对象我们经常用来进行统一的权限判断、日志等处理。
例子代码:

    public class MyModule : IHttpModule
    
{
        
public void Init(HttpApplication application)
        
{
            application.BeginRequest 
+= new EventHandler(application_BeginRequest);
        }


        
void application_BeginRequest(object sender, EventArgs e)
        
{
            ((HttpApplication)sender).Response.Write(
"Copyright @Gspring<br/>");
        }


        
public void Dispose()
        
{
        }

    }

web.config中配置:

      <httpModules>
        
<add name="test" type="HttpHandle.MyModule, HttpHandle"/>
      
</httpModules>

在Init方法中可以注册很多application的事件,我们的例子就是在开始请求的时候加入自己的代码,将版权声明加到页面的头部


二、HttpHandler

这个对象经常用来加入特殊的后缀所对应的处理程序,比如可以限制.doc的文件只能给某个权限的人访问。
Asp.Net中的Page类就是一个IHttpHandler的实现
例子代码:

    public class MyHandler : IHttpHandler
    
{
        
public void ProcessRequest(HttpContext ctx)
        
{
            ctx.Response.Write(
"Copyright @Gspring<br/>");
        }

        
public bool IsReusable
        
{
            
get return true; }
        }

    }

web.config中配置:

      <httpHandlers>
      
<add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
      
</httpHandlers>

这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么所有的aspx页面都不能正常运行了

三、HttpHandlerFactory

这个对象也可以用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更加强大,在系统的web.config中就是通过注册HttpHandlerFactory来实现aspx页面的访问的:

      <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>

HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。
例子代码:

    public class MyHandlerFactory : IHttpHandlerFactory
    
{
        
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        
{
            PageHandlerFactory factory 
= (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);
            IHttpHandler handler 
= factory.GetHandler(context, requestType, url, pathTranslated);

            
//执行一些其它操作
            Execute(handler);

            
return handler;
        }


        
private void Execute(IHttpHandler handler)
        
{
            
if (handler is Page)
            
{
                
//可以直接对Page对象进行操作
                ((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);
            }

        }


        
void MyHandlerFactory_PreLoad(object sender, EventArgs e)
        
{
            ((Page)sender).Response.Write(
"Copyright @Gspring<br/>");
        }


        
public void ReleaseHandler(IHttpHandler handler)
        
{
        }

    }

web.config中配置:

      <httpHandlers>
      
<add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
      
</httpHandlers>

在例子中我们通过调用系统默认的PageHandlerFactory类进行常规处理,然后在处理过程中加入自己的代码,可以在Page对象上附加自己的事件处理程序。

附一个小的恶作剧:
我们可以开发好aspx页面,然后把web应用程序发布后把所有的aspx文件的后缀都改为spring,再在web.config中加入配置:

      <httpHandlers>
      
<add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
      
</httpHandlers>
      
<compilation>
        
<buildProviders>
          
<add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
        
</buildProviders>
      
</compilation>

buildProviders是用来指定spring后缀的编译程序,我们把它设置成和aspx一致就可以了。如果在IIS中发布的话还需要在应用程序配置中加入spring的后缀映射。
然后我们就可以通过 http://../.../*.spring来访问我们的网站了