1 向客户端发送响应的工作都由处理程序完成

2 任何实现System.web.ihttpHandler接口的类都可以作为传入的http请求的目标

3 如果需要重复使用自定义处理程序对象,需要创建自定义处理程序工厂。

4 如何创建自定义处理程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Handlers
{
public class CustomHandler : IHttpHandler
{
public bool IsReusable
{
get
{
return false;
}
} public void ProcessRequest(HttpContext context)
{
string time = DateTime.Now.ToShortTimeString();
if (context.Request.CurrentExecutionFilePathExtension==".json")
{
context.Response.ContentType = "application/json";
context.Response.Write(string.Format("{{\"time\":\"{0}\"}}", time));
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write(string.Format("<span>{0}</span>", time));
}
}
}
}

在web.config文件中注册自定义的处理程序

<system.webServer>
<handlers>
<add name="customJson" path="*.json" verb="GET" type="Handlers.CustomHandler"/>
<add name="customText" path="Time.text" verb="*" type="Handlers.CustomHandler"/>
</handlers>
</system.webServer>

5 如何创建自定义的处理程序工厂

自定义处理程序工厂是实现IHttpHandlerFactory接口的类,他负责生成用于响应的IHttpHandler对象

首先创建实现IHttpHandlerFactory接口的实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Handlers
{
public class InstanceControlFactory : IHttpHandlerFactory
{
private int factoryCounter = ;
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
return new InstanceControlHandler(factoryCounter++);
} public void ReleaseHandler(IHttpHandler handler)
{
throw new NotImplementedException();
}
} public class InstanceControlHandler : IHttpHandler
{
private int v; public InstanceControlHandler(int v)
{
this.v = v;
} public bool IsReusable
{
get
{
return false;
}
} public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(string.Format("the counter value is {0}",v));
}
}
}

其次在web.config文件中注册实现IHttpHandlerFactory接口的类

  <system.webServer>
<handlers>
<add name="customJson" path="*.json" verb="GET" type="Handlers.CustomHandler"/>
<add name="customText" path="Time.text" verb="*" type="Handlers.CustomHandler"/>
<add name="InstanceControl" path="*.instance" verb="*" type="Handlers.InstanceControlFactory"/>
</handlers>
</system.webServer>

6 如何重复的使用处理程序,将同一个处理程序对象应用于多个不同的请求

asp.net frameworke处理程序的作用的更多相关文章

  1. .net mvc 站点自带简易SSL加密传输 Word报告自动生成(例如 导出数据库结构) 微信小程序:动画(Animation) SignalR 设计理念(一) ASP.NET -- WebForm -- ViewState ASP.NET -- 一般处理程序ashx 常用到的一些js方法,记录一下 CryptoJS与C#AES加解密互转

    .net mvc 站点自带简易SSL加密传输   因项目需要,传输数据需要加密,因此有了一些经验,现简易抽出来分享! 请求:前端cryptojs用rsa/aes 或 rsa/des加密,后端.net ...

  2. ASP.NET -- 一般处理程序ashx

    ASP.NET  --   一般处理程序ashx 如果在一个html页面向服务器端请求数据,可用ashx作为后台页面处理数据.ashx适合用作数据后台处理,相当于WebForm中的aspx.cs文件或 ...

  3. asp.net中处理程序调用HttpContext.Current.Session获取值出错

    asp.net中处理程序调用System.Web.HttpContext.Current.Session获取Session时提示错误:未将对象引用设置到对象的实例. 解决办法:在处理程序文件类中实现I ...

  4. ASP.Net各个命名空间及作用

    (引用自hungerw的博客) 命名空间 描述 Microsoft.CSharp        支持C#语言编译和生成代码 System                            包含了基 ...

  5. asp.net一般处理程序利用反射定位方法

    asp.net的一般处理程序我想大家用得都不少,经常会如下如下的代码: using System; using System.Collections.Generic; using System.Lin ...

  6. asp.net 一般处理程序接收上传文件的问题

    在使用Html+ashx处理文件上传时,遇到上传文件超过4M的问题,首先HTML代码如下: <!DOCTYPE html> <html> <head> <me ...

  7. 初识ASP.NET---一般处理程序

    问题来源: 今天在敲一个小的demo,利用Jquery实现级联下拉框,敲的过程中发现不管怎么和源代码对比都无法显示想要的功能. 这才想着原来是没有写后台代码,询问一清同学的时候,他告诉我能够利用ASP ...

  8. asp.net ashx处理程序中switch case的替代方案总结

    目录 1.用委托字典代替switch...case; 2.利用反射替代switch...case: 3.比较两种方案 4.其他方案 4.说明 5.参考 在开发 asp.net 项目中,通常使用一般处理 ...

  9. asp.net identity UserSecurityStamp 的作用

    UserSecurityStamp 主要是用来对用户安全相关信息做一个快照. 在使用asp.net identity 的 CreateAsync(TUser user) 创建一个用户的时候,如果开启了 ...

随机推荐

  1. SQL-W3School-基础:SQL SELECT 语句

    ylbtech-SQL-W3School-基础:SQL SELECT 语句 1.返回顶部 1. 本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于 ...

  2. Kibana Query Language(KQL)

    语法: 官方文档 If you’re familiar with Kibana’s old lucene query syntax, you should feel right at home wit ...

  3. ES6深入浅出_汇总贴

    H:\BaiDu\ES6深入浅出-wjw ES 6 新特性一览:https://frankfang.github.io/es-6-tutorials/ 我用了两个月的时间才理解 let https:/ ...

  4. EasyUI之toolTip

    <a class="easyui-tooltip" title="提示框" href="http://www.baidu.com"&g ...

  5. iOS-保存图片到相册

    //保存      UIButton *saveBtn = [[UIButton alloc] init];    //    saveBtn.frame = CGRectMake((screenWi ...

  6. windows下初安装xgboost

    1.先检查一下自己的版本,如图python3.6,win64 我的之前环境是已经安装了anaconda. 2.去相关的网站下载 3.把下载的文件拷到此目录下(同pip在一个目录下) 4.cmd在此目录 ...

  7. jmeter5实现文件上传接口测试

    背景:在公司做接口自动化编写过程中,遇到需要测试一个在线下载导入模板的接口,之前都没有接触过关于文件上传下载的接口测试,此处做个记录,为后续工作开展做个参考. 步骤: 打开浏览器按F12 手动进行文件 ...

  8. 集群架构01.Nginx初步安装配置

    1.切换163yum 源,环境介绍 [root@moban ~]# cat /etc/redhat-release CentOS release 6.5 (Final) mv CentOS-Base. ...

  9. Apache服务器安装SSL证书

    Apache服务器安装SSL证书 在证书控制台下载Apache版本证书,下载到本地的是一个压缩文件,解压后里面包含_public.crt文件是证书文件,_chain.crt是证书链(中间证书)文件,. ...

  10. arduino系列文章

    arduino系列文章 1.Arduino基础入门篇-进入Arduino的世界 2.关于使用Arduino做开发的理解 3.详解Arduino Uno开发板的引脚分配图及定义(重要且基础) 4.Ard ...