On IHttpHandler

Or when the Web application development interface development, processing request interface IHttpHandler everywhere, so this time we simply talk about this interface.

Commonly used when two ASP.NET Http request response processing interface, and are IHttpHandler IHttpModule.

1, IHttpHandler

Usually for processing a specific request class, for example, each * .asp, * .aspx file separately.

2、IHttpModule

Commonly used to process request operation so common needs, such as requests for pages so that some of the same check.

We first look at IIS server procedure when the corresponding Http request.

After the request arrives, achieved after processing pass HttpModule recall ProcessRequest HttpHandler () method will be specifically appropriate. Therefore, do not difficult to understand why some HttpModule for all requests for general inspection operation, and will process the request on a specific class HttpHandler class.

 

A, IHttpHandler

First we look at IHttpHandler interface design.

IHttpHandler interface has only two members:

 public interface IHttpHandler
 {
     bool IsReusable { get; }
     void ProcessRequest(HttpContext context); 
 }

1, IsReusable: HttpHandler identify the objects can be used by other instances, it will be generally set to True.

2, ProcessRequest (): method specifically in response to the request, as long as we will operate on the specific business logic here to.

practice:

Create a new Web project, add a class 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文件中的几个属性做一下说明:

1、path:表示URL匹配,如*.ray这表示该Handler会响应所以以".ray"结尾的URL请求。

2、verb:表示请求方法,如Get/Post,使用*则表示所以匹配所有。

3、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);
}

1、GetHandler(): 返回一个实现了IHttpHandler接口的实例。

2、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的类名约定一下对应关系即可。具体实现方式不在说明。


 

Guess you like

Origin www.cnblogs.com/xiaomowang/p/11124518.html