HTTP Handlers in ASP.NET

原创 2004年09月12日 19:55:00
HTTP Handlers in ASP.NET
Author Date Of Submission User Level
Sivakumar N 08/24/2004 Intermediate

Download: Examplehandler.rar 3Kb


The low level Request and Response API to service incoming Http requests are Http Handlers in Asp.Net. All handlers implement the IHttpHandler interface, which is located in the System.Web namespace. Handlers are somewhat analogous to Internet Server Application Programming Interface (ISAPI) extensions.

In this article, I will explain how to extend the functionality of web server by using Http handlers and How to protect files using Http handlers.

Methods in Http Handler

The following are the methods in Http Handlers.
Method Name Description
ProcessRequest Used o call Http Requests.
IsReusable To check the reusability of same instance handler with a new request of same type.

Configuring HTTP Handlers

The <httpHandlers> configuration section handler is responsible for mapping incoming URLs to the IHttpHandler or IHttpHandlerFactory class. It can be declared at the computer, site, or application level. Subdirectories inherit these settings.

Administrators use the <add> tag directive to configure the <httpHandlers> section. <Add> directives are interpreted and processed in a top-down sequential order. Use the following syntax for the <httpHandler> section handler:

<add verb="[verb list]" path="[path/wildcard]" type="[COM+ Class], [Assembly]" validate="[true/false]" />
<remove verb="[verb list]" path="[path/wildcard]" />
<clear />

Creating HTTP Handlers

To create an HTTP handler, you must implement the IHttpHandler interface. The IHttpHandler interface has one method and one property with the following signatures:
void ProcessRequest(HttpContext);
bool IsReusable {get;}

Customized Http Handler

By customizing http handlers, new functionalities can be added to Web Server. Files with new extensions like .text for a text file can be handled by Web Server by using http handlers. The future of customization can lead to hosting .jsp pages in IIS by finding adequate ISAPI extensions. The following steps are involved to create customized http handler:
1. Create a C# class library as 揈xamplehandler?br> 2. Name class as 揌andlerclass.cs?/font>

using System;
using System.Web;
using System.Web.SessionState;
namespace ExampleHandler
/// <summary>
/// Summary description for Examplehandler.
/// </summary>
public class Handlerclass : IHttpHandler
public Handlerclass()
// TODO: Add constructor logic here

#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
HttpResponse objResponse = context.Response ;
HttpSessionState objSession = context.Session ;
objResponse.Write("<html><body><h1>Hello World from Handler") ;
objResponse.Write("</body></html>") ;

public bool IsReusable
return true;

Compile it and place it in the bin directory of TestApp project.

Step 2

Register this handler by adding the following text in the web.config file:

<add verb="*" path="*.text" type=" ExampleHandler.Handlerclass, ExampleHandler "/>

Step 3

Go to Internet Information Services and select Default Web Site. Right Click and Select Properties. Select Home Directory and click on Configuration. The Following Screen will appear:


Click on Add and give executable path and new extension and click OK.


Close IIS and Run TestApp website by using the URL


The output will be as follows:



The sensitive files can be protected by Http Forbidden Handler. The Database driven web sites using MS Access, the .mdb file has to be protected. To protect the .mdb files, we must follow the two steps given below:

1. Map .mdb file in IIS

2. Register the file extension in web.config with HttpForbiddenHandler.

In the Web.Config file, Add this Http handler section:

<add verb="*" path="*.mdb" type="System.Web.HttpForbiddenHandler"/>


The Http Handlers are often useful when the services provided by the high-level page framework abstraction are not required for processing the HTTP request. Common uses of handlers include filters and CGI-like applications, especially those that return binary data.

HTTP Handlers and HTTP Modules in ASP.NET

  HTTP Handlers and HTTP Modules in ASP.NETBy Mansoor Ahmed Siddiqui ...
  • yifan268
  • yifan268
  • 2007年05月28日 00:07
  • 552

Asp.Net构架(Http请求处理流程)、Asp.Net 构架(Http Handler 介绍)、Asp.Net 构架(HttpModule 介绍)

Asp.Net构架(Http请求处理流程) - Part.1 引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是站在一个比较高的层次上讲解Asp.Net。他们耐心、细致地告诉你如何一步步拖...
  • jiuqiyuliang
  • jiuqiyuliang
  • 2014年01月24日 09:52
  • 9649

ASP.NET中的Http Handler

一、关于http handler    ASP.NET的请求过程是基于一个管道(pipeline)模型的,ASP.NET会把所有的http请求(Requests)都发送给这个管道里的http组件(mo...
  • possible_Y
  • possible_Y
  • 2004年04月02日 15:46
  • 6377 MVC防止图片盗链的实现方法,通过自定义RouteHandler来操作

本文告诉你如何在ASP.NET MVC中实现一个自定义RouteHandler来防止其他人盗链你的图片. 首先,我们来回顾一下当一个请求发往 MVC站点时的情景,IIS收到请求并将请求...
  • chengly0129
  • chengly0129
  • 2012年08月22日 16:12
  • 828

ASP.NET异步请求处理(Asynchronous Http Handlers)

ASP.NET中你可以通过继承IHttpHandler这个接口来实现一个同步(Synchronous)处理用户请求的类。比如你希望对于一切类型为fakephp的请求都通过你的Http Hanlder来...
  • blackvii
  • blackvii
  • 2008年01月05日 16:45
  • 2398

ASP.NET异步请求处理(Asynchronous HTTP Handlers)

ASP.NET异步请求处理(Asynchronous HTTP Handlers) ASP.NET中你可以通过继承IHttpHandler这个接口来实现一个同步(Synchronou...
  • nic7968
  • nic7968
  • 2012年05月28日 23:54
  • 325

ASP.NET MVC3 中的UrlRewrite

ASP.NET MVC3中自带的Url重写工具,今天和大家一起分享一下简单的实现过程。 最后要实现的结果:以二级域名的形式传递城市拼音并解析。如下图所示: 第一步:创建一个UrlProvide...
  • dyllove98
  • dyllove98
  • 2013年03月25日 13:01
  • 1706

HTTP Handlers and HTTP Modules Overview

HTTP Handlers and HTTP Modules Overview .NET Framework 4 An ASP.NET HTTP handler is the process (...
  • bdstjk
  • bdstjk
  • 2011年11月24日 12:50
  • 1774


  • pandeng4639088
  • pandeng4639088
  • 2015年07月08日 11:59
  • 1796


说明如下: verb可以是“POST”或 “GET”,表示对GET或POST的请求进行处理。“*”表示对所有的请求进行处理。 path指明相对应的文件进行处理,"*.aspx" 表示对发给所有...
  • Joyhen
  • Joyhen
  • 2013年04月01日 17:06
  • 7611
您举报文章:HTTP Handlers in ASP.NET