日期:2011-02-15  浏览次数:20532 次

Creating HttpHandlers and HttpModules
Introduction
ASP.NET allows you to extend its functionality in two main ways :
HttpHandlers
HttpModules
Http handlers are special applications that typically handle files with certain extension. For example when you request a file with extension .asp IIS routes it to ASP processor. But what if I want to handle files with my own extensions say .bipin? Http handlers allow us to do just that. Now, you might be thinking what is the use of a new file extension? Consider a case where you want to generate graphics on the fly. In such cases you will write an Http handler that will do your task. Note that Http handlers are typically called for the specific file extensions they are designed for. If you have worked with ISAPI extensions in past you will find this concept very familiar.
Http modules are similar to Http handlers in that they allow you to tweak with the request and response. However, they are typically executed for every request irrespective of the file type. If you have worked with ISAPI filters before you will find this concept very familiar.

In this article we will see how to create Http Handlers and Http Modules and use them in your ASP.NET pages.

IHttpHandler interface
In order to create Http handler you have to create a class that implements IHttpHandler interface. This interface has one method with following signature :
Sub ProcessRequest(Context as HttpContext)
It also has one read only property with following signature :
Public ReadOnly Property IsReusable() As Boolean

The ProcessRequest method is used to do all of your processing. The context parameter provides access to various objects like Request and Response. The IsReusable property tells whether another requests can use the same instance of Http handler.

Creating the class implementing IHttpHandler
Following is a class that implements IHttpHandler interface.

Public Class MyHttpHandler     
Implements IHttpHandler     Public Sub ProcessRequest(ByVal context As HttpContext)             Implements IHttpHandler.ProcessRequest         context.Response.Write("hello world")     End     Sub Public ReadOnly Property IsReusable() As Boolean             Implements IHttpHandler.IsReusable         Get            Return True         End Get     End Property End Class
Here, we are simply outputting a string "Hello World" for each request handled by this Http handler. You can perform any complex task as per your requirements.

Configuring our Http handler
After you create your Http handler class you should configure your web application so that specific requests will be handled by the handler. To accomplish this you will modify web.config file as follows :

<httpHandlers>
<add verb="*" path="hello.aspx"
type="SampleWebApplication.MyHttpHandler,SampleWebApplication" />
</httpHandlers>

Here, verb attribute indicates GET, POST or * (all). The path attribute  indicates the resource to be handled. In our case we have specific file hello.aspx. Type attribute indicates the fully qualified name of the class and name of assembly respectively.

In case you have to handle different extension say *.bipin then in addition to configuring in web.config (as shown above) you also need to add the extension in IIS. This allows IIS to forward request for specific extension to ASP.NET processor which in turn forwards it