在Web应用开发或接口开发时,处理请求接口IHttpHandler随处可见,那么咱们此次来简单聊一下这个接口。设计模式
ASP.NET响应Http请求时经常使用的两个处理接口,分别是IHttpHandler和IHttpModule。安全
一、IHttpHandler服务器
通常用来处理一类特定的请求,好比对每一个*.asp, *.aspx文件的分别处理。url
二、IHttpModulespa
一般用来处理因此请求共同须要的操做,好比对因此请求页面进行某些相同的检查功能。设计
咱们先来看一下IIS服务器在相应Http请求时的处理步骤。code
请求到达以后,实现通过HttpModule处理以后再调用HttpHandler的ProcessRequest()方法进行具体相应的。所以,也不难理解为何说在HttpModule中作一些对全部请求通用的检查操做,而将特定类请求的处理放在HttpHandler类中。对象
首先咱们来看一下IHttpHandler接口设计。blog
IHttpHandler接口只有两个成员:接口
public interface IHttpHandler { bool IsReusable { get; } void ProcessRequest(HttpContext context); }
一、IsReusable:标识该HttpHandler对象可否被其余实例使用,通常咱们将其置为True。
二、ProcessRequest():具体响应请求方法,咱们只要将具体的业务逻辑操做放在这里便可。
实践:
新建一个Web工程,添加一个Handler类:
public class RayHandler : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write("Asp.Net HttpHandler Demo. -- ."); } }
而后,咱们须要在Web.config文件中添加如下配置:
<handlers> <add name="test" path="*.ray" verb="*" type="WebApplication2.RayHandler,WebApplication2"/> </handlers>
对config文件中的几个属性作一下说明:
一、path:表示URL匹配,如*.ray这表示该Handler会响应因此以".ray"结尾的URL请求。
二、verb:表示请求方法,如Get/Post,使用*则表示因此匹配全部。
三、type:指示Handler类的类型,上面的config文件中,WebApplication2.RayHandler是类名,WebApplication2是指Bin目录下该该程序集的名称(不带.dll后缀)。
启动站点,输入以".ray"结尾的URL,能够看到以下结果:
问题:
有时候咱们可能须要处理多种不一样的后缀,一个后缀对应一个Handler类,这时咱们的Web.config文件看起来就是这样了:
<handlers> <add name="test" path="*.ray" verb="*" type="WebApplication2.RayHandler,WebApplication2"/> <add name="test1" path="*.rss" verb="*" type="WebApplication2.RssHandler,WebApplication2"/> </handlers>
若是咱们有不少的HttpHandler实现类,那么咱们的Web.config文件配置势必会显得很冗长。
解决问题:
为了解决以上问题,须要使用IHttpHandlerFactory。一看这个接口的名字,猜想是以工厂模式实现的。首先咱们来看一下他的接口构成:
IHttpHandlerFactory
public interface IHttpHandlerFactory{ IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated); void ReleaseHandler(IHttpHandler handler); }
一、GetHandler(): 返回一个实现了IHttpHandler接口的实例。
二、ReleaseHandler():使得Factory能够重复使用一个已经存在Handler实例。
以上述ray,rss请求为例,实现Factory类:
public class HandlerFactory : IHttpHandlerFactory{ public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){ IHttpHandler handler = null; string path = context.Request.PhysicalPath; switch(Path.GetExtension(path)){ case ".ray": handler = new RayHandler(); break; case ".rss": handler = new RssHandler(); break; default: break; } return handler; } public void ReleaseHandler(IHttpHandler handler){ //void } }
这时,在Web.config中的配置以下:
<handlers> <add name="test1" path="*.ray,*.rss" verb="*" type="WebApplication2.FactoryHandler,WebApplication2"/> </handlers>
使用了IHttpHandlerFactory,那么咱们的config文件的配置相对就简化了不少。
问题:
若是程序后续须要增长对新后缀的处理方法,就须要修改GetHandler()中的Switch语句,可能引起错误或带来其余安全隐患,这样作也违反了设计原则中的开放封闭原则。那么,如何才可以实如今后续扩展时,保持HandlerFactory类不变呢?
解决问题:
答案确定是能够的。 熟悉设计模式的应该明白这里是一个简单工厂模式,要实现前面的功能咱们用叫高级点的设计模式是能够实现的。
而在这里,咱们还能够用C#语言的语言特性--反射。 经过C#的反射机制,咱们根据URL的后缀来反射获取对应的Hanlder类型,只要咱们将URL的后缀名跟Handler的类名约定一下对应关系便可。具体实现方式不在说明。