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 }

转载于:https://www.cnblogs.com/Loofah/archive/2012/06/08/2542034.html

实现HttpHandlerFactory的方法相关推荐

  1. 在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群

    作者:Jeffrey Hunter 了解如何以低于 2,700 美元的费用在 Oracle Enterprise Linux 上安装并配置 Oracle RAC 11g 第 2 版开发集群. 本指南中 ...

  2. Java面试题大全2021版

    一.Java 基础 JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Run ...

  3. C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...

  4. Kotlin协程使用,协程使用注意事项,协程中的await方法使用|不使用suspend使用协程

    参见 码云 协程使用方法一 (Dispatchers调度器模式) 指定不同线程.同线程会挂起并阻塞(挂起是不影响主线程执行,阻塞是同样的IO线程会阻塞) withContext(Dispatchers ...

  5. IDEA中将代码块封装为方法,IDEA代码重构快捷键

    IDEA中将代码块封装为方法 选中要转换的代码块,快捷键: Windows快捷键:Alt + Shift + M Mac快捷键:Alt + Command + M 如图:

  6. IDEA自动生成对象所有set方法

    idea中有一款插件能够生成对象所有的set方法,GenerateAllSetter :下载地址 步骤1:将下载好的压缩包放在自己记得的文件夹中,在idea中进行导入 步骤2:在本地选中刚才的压缩包, ...

  7. List元素互换,List元素转换下标,Java Collections.swap()方法实例解析

    Java Collections.swap()方法解析 jdk源码: public static void swap(List<?> list, int i, int j) {// ins ...

  8. java1.8新增超实用Map方法——Map.getOrDefault()和Map.value()方法详解

    1. Map.getOrDefault() 翻译一下官方解释: java.util.Map<K, V> V getOrDefault(Object key, V defaultValue) ...

  9. spring boot项目 中止运行 最常用的几种方法

    spring boot项目 中止运行 最常用的几种方法: 1. 调用接口,停止应用上下文 @RestController public class ShutdownController impleme ...

最新文章

  1. html在线热点工具,Heatmap.js 强大的 HTML5 画布 WEB 网页在线热点图插件 - 资源分享...
  2. Linux 防火墙开放特定端口 (iptables)
  3. 关闭子页面刷新父页面,不需要弹出确认窗口
  4. 了解一下ES6: 函数简述深浅拷贝
  5. java常量池方法区_Java方法区和运行时常量池溢出问题分析
  6. 魔方机器人需要特制魔方吗_火影忍者手游:如果新春水门金币团购,你会夸魔方良心吗?...
  7. 在SQL Server中取得操作系统文件的最后修改日期 [Z]
  8. 用concat批量生成MySQL查询语句
  9. Spring源码解析目录
  10. element Descriptions 内容超出隐藏 Tooltip展示完整内容
  11. 4G模块 EC03-DNC的常用AT指令操作及模块访问内网方法
  12. Android Studio连接手机教程
  13. 游戏3D美术设计就业前景如何?现在饱和了吗
  14. 【MySQL】DDL语句详解:列类型、列约束及自增列_Unit03
  15. Java面试题总结2020
  16. win10电脑自动配置ipv4地址169怎么办?win10电脑关闭自动配置ipv4地址的方法
  17. spring boot 配置Filter过滤器
  18. jQuery遍历集合
  19. php 数组压缩成一行,php对特定数组进行压缩
  20. python中的flatten_numpy下的flatten()函数用法详解

热门文章

  1. WPF Multi-Touch 开发:惯性效果(Inertia)
  2. 4.2 路由算法与路由协议概述
  3. warnings.filterwarnings(ignore)
  4. vmware下ubuntu安装vmware tool工具及使用鼠标滚轮的方法
  5. 微软IE8浏览器个性化设置技巧
  6. conda 命令和创建tensorflow环境
  7. scrapy中response的方法
  8. 比较器 Comparable 与compartor 的区别及理解
  9. 第K短路模板【POJ2449 / 洛谷2483 / BZOJ1975 / HDU6181】
  10. Application Virtualization 4.5 部署之(一)