JavaScript Editor jscript editor     Web designer 



Main Page

The IHttpHandlerFactory interface creates and manages HTTP handlers for processing requests. It is possible, therefore, to create a class that implements the IHttpHandlerFactory interface, and then use that class as an HTTP handler. This can allow finer control over the processing of an HTTP request by mapping a URL to an HTTP handler factory that creates different handlers based on a complex set of conditions. For example, with an HTTP handler factory you can create a limited number of HTTP handler objects that access expensive or limited resources, such as database connections, and then reuse those handler objects in future requests.

In the following example, an HTTP handler factory is used to create two handlers for resources that are identified with the extension .sample — one for an HTTP GET request and one for an HTTP POST request. The first is an instance of the handler created in How to: Create Synchronous HTTP Handlers; the second is an instance of the handler created in How to: Create an Asynchronous HTTP Handler.

Creating a Custom HTTP Handler Factory

To create a HandlerFactory HTTP handler factory class

  1. Create a class called HelloWorldHandler in your Web site's App_Code directory.

  2. Add the following code to your class file.

    Visual BasicВ CopyCode imageCopy Code
    Imports System
    Imports System.Web
    
    Class HandlerFactory
        Implements IHttpHandlerFactory
    
        Public Function GetHandler(ByVal context As HttpContext, _
                ByVal requestType As String, ByVal url As [String],_ 
                ByVal pathTranslated As [String]) As IHttpHandler _
                Implements IHttpHandlerFactory.GetHandler
            Dim handlerToReturn As IHttpHandler
            Dim requestType as String = _      
                context.Request.RequestType.ToLower()
            If "get" = requestType Then
                handlerToReturn = New HelloWorldHandler()
            Else
                If "post" = requestType Then
                    handlerToReturn = New HelloWorldAsyncHandler()
                Else
                    handlerToReturn = Nothing
                End If
            End If
            Return handlerToReturn
        End Function
    
        Public Sub ReleaseHandler(ByVal handler As IHttpHandler) _
            Implements IHttpHandlerFactory.ReleaseHandler
        End Sub
    
        Public ReadOnly Property IsReusable() As Boolean
            Get
                Return False
            End Get
        End Property
    End Class

    C#В CopyCode imageCopy Code
    using System;
    using System.Web;
    
    class HandlerFactory : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, 
            string requestType, String url, String pathTranslated)
        {
            IHttpHandler handlerToReturn;
            if ("get" == context.Request.RequestType.ToLower())
            {
                handlerToReturn = new HelloWorldHandler();
            }
            else if ("post" == context.Request.RequestType.ToLower())
            {
                handlerToReturn = new HelloWorldAsyncHandler();
            }
            else
            {
                handlerToReturn = null;
            }
            return handlerToReturn;
        }
        public void ReleaseHandler(IHttpHandler handler)
        {
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    The code implements the GetHandler method of the IHttpHandlerFactory interface and returns the synchronous handler created if the request is a GET request. If the request is a POST request, it returns the asynchronous handler interface.

Creating Custom HTTP Handlers

The custom HTTP handler factory returns either the synchronous handler created in How to: Create Synchronous HTTP Handlers, or the asynchronous handler created in How to: Create an Asynchronous HTTP Handler. You must create both the synchronous HelloWorldHandler class and the asynchronous HelloWorldAsyncHandler class in order for the custom HTTP handler factory to be able to return those handlers.

To create the HelloWorldHandler and the HelloWorldAsyncHandler classes

  1. Create a class named HelloWorldHandler in your Web site's App_Code directory.

  2. Add the code from How to: Create Synchronous HTTP Handlers to the class file.

  3. Create a class named HelloWorldAsyncHandler in your Web site's App_Code directory.

  4. Add the code from How to: Create an Asynchronous HTTP Handler to the class file.

Registering a Custom HTTP Handler Factory

After you have created the custom HTTP handler factory class, you must register it in your Web site's Web.config file. This allows ASP.NET to use the handler factory class to service requests made to resources with the .sample file name extension.

To register a custom HTTP handler in the Web.config file

  1. Add a Web.config file to your Web site if one does not already exist.

  2. Add the following highlighted element to the Web.config file.

    В CopyCode imageCopy Code
    <configuration>
      <system.web>
        <httpHandlers>
          <add verb="GET,POST" path="*.sample"
            type="HandlerFactory" />
        </httpHandlers>
      </system.web>
    </configuration>

    The code registers the handler with the class name and the handler name of HandlerFactory.

Configuring IIS 6.0 for an HTTP Handler Extension

Internet Information Services (IIS) passes requests for only certain file types to ASP.NET to service. By default, files with file name extensions such as .aspx, .ascx, .asmx, are already mapped in IISВ 6.0 to the ASP.NET ISAPI extension (Aspnet_isapi.dll). However, if you want ASP.NET to handle custom URL extensions, you must map the extensions in IIS. For more information, see ASP.NET Life Cycle.

To map the .sample file name extension to ASP.NET in IIS 6.0

  1. Open Internet Information Services (IIS) Manager.

  2. Right-click the name of your application, and then click Properties.

    NoteNote

    For instructions for creating an ASP.NET application, see How to: Create and Configure Local ASP.NET Web Sites in IIS.

  3. Click the Virtual Directory tab, and then click Configuration.

  4. On the Mappings tab, click Add.

    The Add/Edit Application Extension Mapping dialog box is displayed.

  5. In the Executable box, type or browse to the file Aspnet_isapi.dll. By default, the file is in the following location.

    В CopyCode imageCopy Code
    %windows%\Microsoft.NET\Framework\version\
    NoteNote

    You can get the complete path and file name from other mappings, such as the mapping to .aspx files.

  6. In the Extension box, type .sample.

  7. Clear the Verify that file exists check box.

  8. Click OK and then close IIS Manager.

Testing the Custom HTTP Handler Factory

After you have created and registered your custom HTTP handler factory, you can test it by creating an HTML page that can request a resource that has a .sample file name extension.

To test your custom HTTP handler factory

  1. Create an HTML page (with a file name extension of .htm) in your application.

  2. In the body section of the page, add the following code.

    В CopyCode imageCopy Code
    <form action="Sample.sample" method="get">
      <input type="submit" value="Submit to Sample.sample via Get" />
    </form>
    <br />
    <form action="Sample.sample" method="post">
      <input type="submit" value="Submit to Sample.sample via Post" />
    </form>
  3. Request the HTML page in your browser.

  4. Click one of the buttons.

    If you click the first button the HTTP handler factory will respond to the request by creating and calling a synchronous HTTP handler. If you click the second button the HTTP handler factory will respond to the request by creating and calling an asynchronous HTTP handler.

See Also



JavaScript Editor jscript editor     Web designer