• Asp.Net IHttpHandler介绍


    ASP.NET响应Http请求时常用的两个处理接口是IHttpHandler和IHttpModule。

    一般的,IHttpHandler用来处理一类特定的请求,比如对每个*.asp, *.aspx文件的分别处理。而IHttpModule通常用来处理所以请求共同需要的操作,比如对所以请求页面进行某些相同的检查功能。

    我们先来看一下IIS服务器在相应Http请求时的处理步骤。 ASP.NET中有管线(Pipeline)这个概念,意指每个ASP.NET请求在IIS中会有一系列相应操作串联起来形成的一条类似线一样的序列。

    ASP.NET管线介绍

    我们来看一下管线的处理时序图:

    从图中看出,请求到达之后,实现经过HttpModule处理之后再调用HttpHandler的ProcessRequest()方法进行具体相应的。因此,也不难理解为什么说在HttpModule中做一些对所有请求通用的检查操作,而将特定类请求的处理放在HttpHandler类中。

    代码实践

    IHttpHandler

    笔者最近在项目中接触到用IHttpHandler来实现对客户端接口调用的处理,这里便来简单探讨下基于IHttpHandler的简单接口设计。

    IHttpHandler接口只有两个成员:  

    1 public interface IHttpHandler
    2 {
    3     bool IsReusable { get; }
    4     void ProcessRequest(HttpContext context); 
    5 }

    IsReusable属性是标识改HttpHandler对象能否被其他实例使用,一般我们将其置为True。 ProcessRequest()方法则是具体的响应请求方法,我们只要将具体的业务逻辑操作放在这里即可。

    首先,新建一个Web工程,添加一个Handler类:

    复制代码
     1 public class RayHandler : IHttpHandler
     2 {
     3     public bool IsReusable
     4     {
     5         get { return true; }
     6     }
     7 
     8     public void ProcessRequest(HttpContext context)
     9     {
    10         context.Response.Write("Asp.Net HttpHandler Demo. -- .");
    11     }
    12 }
    复制代码

    RayHandler类实现了IHttpHandler接口的ProcessRequest()函数,这里只是直接输出一条文本。

    然后,我们需要在Web.config文件中添加以下配置:

    <handlers>
      <add name="test" path="*.ray" verb="*" type="WebApplication2.RayHandler,WebApplication2"/>
    </handlers>

    path表示URL匹配,如*.ray这表示该Handler会响应所以以".ray"结尾的URL请求,verb表示请求方法,如Get/Post,使用*则表示所以匹配所有。type指示Handler类的类型,WebApplication2.RayHandler是类名,WebApplication2是指Bin目录下该该程序集的名称,如示例中的程序集名称为WebApplication2.dll,且这里不需要制定后缀名。

    启动站点,输入以".ray"结尾的URL,可以看到如下结果:

    IHttpHandlerFactory概述

    有时候我们可能需要处理多种不同的后缀,一个后缀对应一个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文件配置势必会显得很冗长。或者在某些情况下,我们只有当程序运行时才能确定哪个Handler进行响应时,这个时候就需要使用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类:

    复制代码
     1 public class HandlerFactory : IHttpHandlerFactory{
     2     public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
     3         IHttpHandler handler = null;
     4         string path = context.Request.PhysicalPath;
     5         switch(Path.GetExtension(path)){
     6             case ".ray":
     7                 handler = new RayHandler();
     8                 break;
     9             case ".rss":
    10                 handler = new RssHandler();
    11                 break;
    12             default:
    13                 break;
    14         }
    15 
    16         return handler;
    17     } 
    18 
    19     public void ReleaseHandler(IHttpHandler handler){
    20         //void
    21     }
    22 }
    复制代码

    这时,在Web.config中的配置如下:

    <handlers>
        <add name="test1" path="*.ray,*.rss" verb="*" type="WebApplication2.FactoryHandler,WebApplication2"/>
    </handlers> 

    这时就实现了用Factory类来对应不同的具体Handler的功能,简化了配置。

    可扩展的IHttpHandlerFactory

    上述的实现方式中,如果程序后续需要增加对新后缀的处理方法,就需要修改GetHandler()中的Switch语句,同样可能引发错误或带来其他安全隐患。那么,能否实现在后续扩展时,保持HandlerFactory类不变呢?

    答案肯定是可以的。 熟悉设计模式的读者应该明白这里是一个简单工厂模式,要实现前面的功能我们用叫高级点的设计模式是可以实现的。

    而在这里,我们还可以用C#语言的语言特性--反射。 通过C#的反射机制,我们根据URL的后缀来反射获取对应的Hanlder类型,只要我们将URL的后缀名跟Handler的类名约定一下对应关系即可。

    如,我们对GetHandler()重写如下:

    复制代码
     using System.Reflection;
    
     public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
            IHttpHandler handler = null;
            string path = context.Request.PhysicalPath;
            string className = this.GetType().Namespace + Path.GetExtension(path);
            try{
                Type type = Type.GetType(className);
                if(type == null){
                    context.Response.Write(string.Format("找不到"{0}"对应的处理程序",Path.GetExtension(path)));
                }
            
                handler = (IHttpHandler)type.Assembly.CreateInstance(className);
            }catch(Exception ex){
                context.Response.Write(string.Format("请求地址错误:"+ ex.Message));
                handler = null;
            }
            
            return handler;
        } 
    复制代码

    此时,只需要将方法中的Handler类放在HandlerFactory类的同一命名空间下,且在Web.config中正确配置。如,有RayHandler类,那么在应该添加一个如下的配置才能自动匹配:

    <handlers>
        <add name="test1" path="*.RayHandler" verb="*" type="WebApplication2.FactoryHandler,WebApplication2"/>
    </handlers> 

    总结

    本篇简单介绍了ASP.NET中IHttpHandler的用法,在多个Handler请求的处理方面,提供了IHttpHandlerFactory的实现方式,最后,利用C#的反射机制改进了一种可扩展的多请求Handler实现方式。https://www.cnblogs.com/Rayblog/p/6394315.html

  • 相关阅读:
    Spring Boot从入门到精通(一)搭建第一个Spring Boot程序
    程序员未来的出路究竟在哪里?一位老码农的心声
    ​IntelliJ IDEA使用技巧—使用EasyCode插件一键生成代码04期
    浅谈Java后端开发工程师腾讯面试经历分享总结
    Java面试技巧—如何自我介绍
    互联网大厂Java面试题集—Spring boot常见面试题(二)
    互联网大厂Java面试题集—Spring boot面试题(一)
    ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息
    ActiveMQ消息队列从入门到实践(1)—JMS的概念和JMS消息模型
    有多少程序员干到35岁,那么其他人去干什么了?
  • 原文地址:https://www.cnblogs.com/hedianzhan/p/8410439.html
Copyright © 2020-2023  润新知