Documente Academic
Documente Profesional
Documente Cultură
HttpHandlers
HttpHandlers are classes that implement the IHttpHandler and IHttpAsyncHandler interfaces. This section
describes how to create and register HttpHandlers and provides examples of a synchronous handler, an
ASP.NET maps HTTP requests to HttpHandlers. Each HttpHandler enables processing of individual HTTP
URLs or groups of URL extensions within an application. HttpHandlers have the same functionality as
ISAPI extensions with a much simpler programming model. The following table shows examples of the
Handler Description
ASP.NET Page Handler (*.aspx) The default HttpHandler for all ASP.NET pages.
ASP.NET Service Handler (*.asmx) The default HttpHandler for all ASP.NET service pages.
An HttpHandler can be either synchronous or asynchronous. A synchronous handler does not return until it
finishes processing the HTTP request for which it is called. An asynchronous handler usually launches a
process that can be lengthy, and returns before that process finishes.
After writing and compiling the code to implement an HttpHandler, you must register the handler using
Creating HttpHandlers
these interfaces require you to implement the ProcessRequest method and the IsReusable property.
ProcessRequest processes individual HTTP requests. IsReusable specifies whether pooling is supported.
call to process individual HTTP requests, and EndProcessRequest executes cleanup code when the
process ends.
IHttpHandlerFactory interface. This can allow finer control over the processing of an HTTP request by
mapping a URL to an HttpHandler factory that creates different handlers based on a complex set of
1
conditions. For example, with an HttpHandler factory you can create one HttpHandler for a file type if the
After you have created a custom HttpHandler you must configure ASP.NET to map incoming HTTP
requests for specific URLs to your handler. The following procedure describes the steps required.
To register an HttpHandler
1. Compile and deploy the .NET class for the HttpHandler in the \bin directory under the
application's virtual root.
3. <configuration>
4. <system.web>
5. <httpHandlers>
6. <add verb="*" path="MyHandler.New"
7. type="MyHandler.New, MyHandlerAssembly" />
8. <add verb="*" path="*.myNewFileNameExtension"
9. type="MyHandler.Fin, MyHandlerAssembly" />
10. </httpHandlers>
11. <system.web>
</configuration>
Entries within the <httpHandlers> configuration section have three attributes, as shown in the
following table.
Attribute Description
Path The path attribute can contain either a single URL path or a simple wildcard
string (for example, *.aspx).
Verb The verb list can be either a comma-separated list of HTTP methods (for
example, "GET, PUT, POST") or a start-script mapping (for example, the
wildcard character * [an asterisk]).
2
12. Ensure that the HttpHandler file name extension is registered within Internet
Information Services (IIS).
For IIS, you add the required file extension in the AppMapping tab of Application Configuration dialog. You can
add or remove handlers using this dialog.
The dialog associates a specific file extension with a specific handler—in this case, tell IIS to use the ASP.NET
engine (aspnet_isapi.dll). Set the Executable field to the location of the aspnet_isapi.dll file on your server (your
path may vary from the path shown in the figure). Fill in the Extension field with .myext .
1. Synchronous HttpHandler
2. Asynchronous HttpHandler
3. HttpHandler Factory
Synchronous HttpHandler
The following code demonstrates how to process requests for the specific MyApp.MyHello URL within an
ASP.NET application. Next, the configuration file changes required to register the HttpHandler are shown.
HelloWorldHandler.vb
---------------------------------------------------------------------------------------
Imports System.Web
3
Public ReadOnly Property IsReusable() As Boolean Implements
System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Asynchronous HttpHandler
The following code demonstrates how to register and process requests for the specific *.MyAsynch URL
within an ASP.NET application. In this case, the handler launches a time-consuming process, sending a
response to the user at several points to indicate progress. Next, the configuration file changes required to
------------------------------------------------------------------------------------------------------------------------------------------
Imports System
Imports System.Web
Imports System.Threading
Namespace handler
4
Public Function BeginProcessRequest(ByVal context As System.Web.HttpContext,
ByVal cb As System.AsyncCallback, ByVal extraData As Object) As System.IAsyncResult
Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
Dim asynch As New MyAsynchOperation(cb, context)
asynch.KickOffThread()
_context = context
Class SomeResult
Implements IAsyncResult
5
End Sub
6
HttpHandler Factory
You can generate a new handler instance for each HTTP request by creating a class that implements the
IHttpHandlerFactory interface. In the following example, a HttpHandler factory is used to create different
handlers for an HTTP GET request and an HTTP POST request. One of the handlers is an instance of the
synchronous handler in the example above; the other handler is an instance of the asynchronous handler
example.
[Class HandlerFactory]
Imports System
Imports System.Web
Namespace Handlers
Class HandlerFactory
Implements IHttpHandlerFactory
7
Public Sub ReleaseHandler(ByVal handler As IHttpHandler) Implements
IHttpHandlerFactory.ReleaseHandler
End Sub