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. Java同步数据结构之ConcurrentLinkedQueue

    前言 前面介绍的Queue都是通过Lock锁实现的阻塞队列,今天介绍一种非阻塞队列ConcurrentLinkedQueue,所谓非阻塞,其实就是通过CAS代替加锁来实现的高效的非阻塞队列.当许多线程 ...

  2. (转)svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrupted”

    今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrup ...

  3. 如何做一个项目v2.ppt

    链接:https://pan.baidu.com/s/159GQsYK9BcQad3h1CyVUYg 提取码:ivcj 复制这段内容后打开百度网盘手机App,操作更方便哦

  4. 一个老程序员PHP程序员说的话(用来提醒自己)

    我,一个老程序员,也是一个学生,把玩过甚多语言,大多不精.我既非名牌学校,也不是高学历,仅代表一部分比较蛋疼的人.接触PHP也是很早了,从04年的OFSTAR开始的,到现在六年了,期间也接触过不少的语 ...

  5. Hbase 学习记录

    说明: 公司最近要使用HBase 用于(冷)历史数据 存储,和简单离线计算.在一次讨论会上,我发表意见,为什么把近期数据流程热点数据库中,并且继续异步流入到 历史数据库HBase 里面.提供高效查询等 ...

  6. 百度之星2019第一场1002 Game

    思路: 离散化之后dp,dp[i][j]表示完成前i个任务并且处在第j个点所需要的最小代价. 实现: #include <bits/stdc++.h> using namespace st ...

  7. python字典中添加项

    body_daily_close = { "mappings": { "properties": { "trade_date": { &qu ...

  8. 一次记录 java非web项目部署到linux

    1.生成可执行jar 运行提示没有主清单属性 一番查找原因:是因为将项目生成jar包的时候,生成的MANIFEST.MF没有MAIN-CLASS,这里加上就可以了,后面的是项目启动类的完整类名 当然还 ...

  9. redis数据库安装

        一. 简单介绍: REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统. Redis是一个开源的使用A ...

  10. 2019icpc-徐州网络赛

    B. hxc写的 AC code: #pragma GCC optimize(2) #include <cstdio> #include <queue> #include &l ...