IHttpHandlerFactory 接口

定义类工厂为创建新的IHttpHandler对象而必须实现的协定。

命名空间:System.Web

程序集:System.Web.dll

语法:public interface IHttpHandlerFactory

方法:

GetHandler    返回实现IHttpHandler接口的类的实例

ReleaseHandler  使工厂可以重用现有的处理程序实例

 

IHttpHandlerFactory.GetHandler方法

返回实现IHttpHandler接口的类的实例。

语法:

IHttpHandler GetHandler(HttpContext context,string requestType,string url,string pathTranslated)

参数:

context     HttpContext类的实例,它提供对用于为Http请求提供服务的内部服务对象(如Requst、Response、Session和Server)的引用 

requestType   客户端使用的Http数据传输方法(GET或POST)

url        所请求的资源的RawUrl

pathTranslated  所请求资源的PhysicalApplicationPath

 

IHttpHandlerFactory.ReleaseHandler方法

语法:

void ReleaseHandler(IHttpHandler handler)

参数:

handler      要重用的IHttpHandler对象

 

//  Name this C# file HandlerFactoryTest.cs and compile it with the
//  command line: csc /t:Library /r:System.Web.dll HandlerFactoryTest.cs.
//  Copy HandlerFactoryTest.dll to your \bin directory.

namespace test
{
    using System;
    using System.Web;

    //  Factory class that creates a handler object based on a request 
   
//  for either abc.aspx or xyz.aspx as specified in the Web.config file.
    public  class MyFactory : IHttpHandlerFactory
   {
      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name =  " FullTrust ")]
       public  virtual IHttpHandler GetHandler(HttpContext context, 
                                             String requestType, 
                                             String url, 
                                             String pathTranslated)
      {
         String fname = url.Substring(url.LastIndexOf( ' / ')+ 1);
         String cname = fname.Substring( 0, fname.IndexOf( ' . '));
         String className =  " test. " + cname;

         Object h =  null;

          //  Try to create the handler object.
          try 
         {
             //  Create the handler by calling class abc or class xyz.
            h = Activator.CreateInstance(Type.GetType(className));
         }
          catch(Exception e)
         {
             throw  new HttpException( " Factory couldn't create instance  " +
                                     " of type  " + className, e);
         }
          return (IHttpHandler)h;
      }

       //  This is a must override method.
       public  virtual  void ReleaseHandler(IHttpHandler handler)
      {
      }
   }

    //  Class definition for abc.aspx handler.
    public  class abc : IHttpHandler
   {
       public  virtual  void ProcessRequest(HttpContext context)
      {
         context.Response.Write( " <html><body> ");
         context.Response.Write( " <p>ABC Handler</p>\n ");
         context.Response.Write( " </body></html> ");
      }

       public  virtual  bool IsReusable
      {
          get {  return  true; }
      }
   }

    //  Class definition for xyz.aspx handler.
    public  class xyz : IHttpHandler
   {
       public  virtual  void ProcessRequest(HttpContext context)
      {
         context.Response.Write( " <html><body> ");
         context.Response.Write( " <p>XYZ Handler</p>\n ");
         context.Response.Write( " </body></html> ");
      }

       public  virtual  bool IsReusable
      {
          get {  return  true; }
      }
   }
}

/*
______________________________________________________________

To use the handler factory, use the following lines in a 
Web.config file.

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="abc.aspx" type="test.MyFactory,HandlerFactoryTest" />
         <add verb="*" path="xyz.aspx" type="test.MyFactory,HandlerFactoryTest" />
      </httpHandlers>     
   </system.web>
</configuration>
*/

转载于:https://www.cnblogs.com/AngelLee2009/archive/2011/10/29/2228298.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值