实现HttpHandlerFactory的方法

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 
  6 namespace WebApplication1
  7 {
  8    // HttpHandlerFactory 用法
  9     //by Jxb 
 10     //at 2012年6月8日17:00:22
 11 
 12 
 13     //web.config
 14      /*<httpHandlers>
 15           <add verb="*" path="*" type="WebApplication1.MyHandler,WebApplication1"/>
 16       </httpHandlers>
 17     */
 18 
 19     public  class _Customer
 20     {
 21         public _Customer()
 22         {
 23             this.a = 1;
 24             this.b = 2;
 25         }
 26 
 27         int a, b = 0;
 28     }
 29     /// <summary>
 30     /// Summary description for Handler1
 31     /// </summary>
 32 
 33     public class MyHandler : IHttpHandlerFactory
 34     {
 35 
 36 
 37         public static T Factory<T>() where T : new()
 38         {
 39             return new T();
 40         }
 41         #region IHttpHandlerFactory 成员
 42 
 43         public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
 44         {
 45 
 46             string fname = url.Substring(url.IndexOf('/') + 1);
 47             while (fname.IndexOf('/') != -1)
 48                 fname = fname.Substring(fname.IndexOf('/') + 1);
 49             string cname = fname.Substring(0, fname.IndexOf('.'));
 50             string className = "WebApplication1." + cname;
 51 
 52             object h = null;
 53 
 54             try
 55             {
 56 
 57                 //测试工厂方法,相当于下面的Activator.CreateInstance
 58                 //_Customer ab = Factory<_Customer>();
 59 
 60                 // 采用动态反射机制创建相应的IHttpHandler实现类。
 61                 h = Activator.CreateInstance(Type.GetType(className));
 62             }
 63             catch (Exception e)
 64             {
 65                 throw new HttpException("工厂不能为类型" + cname + "创建实例。", e);
 66             }
 67 
 68             return (IHttpHandler)h;
 69         }
 70 
 71         public void ReleaseHandler(IHttpHandler handler)
 72         {
 73 
 74         }
 75 
 76         #endregion
 77     }
 78 
 79 
 80 
 81 
 82 
 83     public class Handler1 : IHttpHandler
 84     {
 85         #region IHttpHandler 成员
 86 
 87         public bool IsReusable
 88         {
 89             get { return true; }
 90         }
 91 
 92         public void ProcessRequest(HttpContext context)
 93         {
 94             context.Response.Write("<html><body><h1>来自Handler1的信息。</h1></body></html>");
 95         }
 96 
 97         #endregion
 98     }
 99 
100     public class Handler2 : IHttpHandler
101     {
102         #region IHttpHandler 成员
103 
104         public bool IsReusable
105         {
106             get { return true; }
107         }
108 
109         public void ProcessRequest(HttpContext context)
110         {
111             context.Response.Write("<html><body><h1>来自Handler2的信息。</h1></body></html>");
112         }
113 
114         #endregion
115     }
116 }

 

posted on 2012-06-08 17:04  魂淡  阅读(270)  评论(0编辑  收藏  举报