asp.net.ph

IHttpHandlerFactory.GetHandler Method

System.Web Namespace   IHttpHandlerFactory Interface


Returns an instance of a class that implements the IHttpHandler interface.

[ VB ]
Function GetHandler ( _
   ByVal context As HttpContext, _
   ByVal requestType As String, _
   ByVal url As String, _
   ByVal pathTranslated As String _
) As IHttpHandler

[ C# ]
IHttpHandler GetHandler (
   HttpContext context,
   string requestType,
   string url,
   string pathTranslated
);

[ C++ ]
IHttpHandler* GetHandler (
   HttpContext* context,
   String* requestType,
   String* url,
   String* pathTranslated
);

[ JScript ]
function GetHandler (
   context : HttpContext,
   requestType : String,
   url : String,
   pathTranslated : String
) : IHttpHandler;

Parameters

context
An instance of the HttpContext class that provides references to intrinsic server objects ( For example, Request, Response, Session, and Server ) used to service HTTP requests.
requestType
The HTTP data transfer method ( GET or POST ) that the client uses.
url
The RawUrl of the requested resource.
pathTranslated
The PhysicalApplicationPath to the requested resource.

Return Value

A new IHttpHandler object that processes the request.

Example

The following example creates custom handler objects in response to a client request for a page named either abc.aspx or xyz.aspx. The handler factory class named hwf creates the appropriate handler object depending on the page requested.

[ VB ] 
' Name this Visual Basic file HandlerFactoryTest.vb and compile it with 
' the command line: vbc /t:Library /r:System.Web.dll HandlerFactoryTest.vb.
' Copy HandlerFactoryTest.dll to your \bin directory.
Imports System
Imports System.Web

Namespace test    
    
    ' Factory class that creates a handler object based on a request 
    ' for either abc.aspx or xyz.aspx as specified in the Web.config file.
    Public Class myFactory
        Implements IHttpHandlerFactory
        
        Public Overridable Function GetHandler ( context As HttpContext, _
        requestType As String, url As String, pathTranslated As String ) _
        As IHttpHandler _
        Implements IHttpHandlerFactory.GetHandler
        
            Dim fname As String = url.Substring ( url.LastIndexOf ( "/"c ) + 1 ) 
            Dim cname As String = fname.Substring ( 0, fname.IndexOf ( "."c ) ) 
            Dim className As String = "test." & cname
            
            Dim h As Object = Nothing
            
            Try ' to create the handler object.
                ' Create by calling class abc or class xyz.
                h = Activator.CreateInstance ( Type.GetType ( className ) ) 
            Catch e As Exception
                Throw New HttpException ( "Factory couldn't create instance " & _
                    "of type " & className, e ) 
            End Try
            Return CType ( h, IHttpHandler ) 
        End Function
        
        
        ' This is a must override method.
        Public Overridable Sub ReleaseHandler ( handler As IHttpHandler ) _
        Implements IHttpHandlerFactory.ReleaseHandler
        
        End Sub
        
    End Class
    
    
    ' Class definition for abc.aspx handler.
    Public Class abc
        Implements IHttpHandler
        
        Public Overridable Sub ProcessRequest ( context As HttpContext ) _
        Implements IHttpHandler.ProcessRequest
        
            context.Response.Write ( "<html><body>" ) 
            context.Response.Write ( "<p>ABC Handler</p>" & _
                Microsoft.VisualBasic.ControlChars.CrLf ) 
            context.Response.Write ( "</body></html>" ) 
        End Sub        
        
        Public Overridable ReadOnly Property IsReusable ( ) As Boolean _
        Implements IHttpHandler.IsReusable
        
            Get
                Return True
            End Get
        End Property
    End Class
     
    ' Class definition for xyz.aspx handler.
    Public Class xyz
        Implements IHttpHandler
        
        Public Overridable Sub ProcessRequest ( context As HttpContext ) _
        Implements IHttpHandler.ProcessRequest
        
            context.Response.Write ( "<html><body>" ) 
            context.Response.Write ( "<p>XYZ Handler</p>" & _
                Microsoft.VisualBasic.ControlChars.CrLf ) 
            context.Response.Write ( "</body></html>" ) 
        End Sub
        
        
        Public Overridable ReadOnly Property IsReusable ( ) As Boolean _
        Implements IHttpHandler.IsReusable
        
            Get
                Return True
            End Get
        End Property
    End Class
End Namespace


[ C# ] 
// Name this C# file HandlerFactoryTest.cs and compile it with the
// command line: csc /t:Library /r:System.Web.dll HandlerFactoryTest.cs.
// Copy HandlerFactoryTest.dll to your \bin directory.

namespace test
{
   using System;
   using System.Web;

   // Factory class that creates a handler object based on a request 
   // for either abc.aspx or xyz.aspx as specified in the Web.config file.
   public class myFactory : IHttpHandlerFactory
   {
      public virtual IHttpHandler GetHandler ( HttpContext context, 
                                             String requestType, 
                                             String url, 
                                             String pathTranslated ) 
      {
         String fname = url.Substring ( url.LastIndexOf ( '/' ) +1 );
         String cname = fname.Substring ( 0, fname.IndexOf ( '.' ) );
         String className = "test." + cname;

         Object h = null;

         // Try to create the handler object.
         try 
         {
            // Create the handler by calling class abc or class xyz.
            h = Activator.CreateInstance ( Type.GetType ( className ) );
         }
         catch ( Exception e ) 
         {
            throw new HttpException ( "Factory couldn't create instance " +
                                    "of type " + className, e );
         }
         return ( IHttpHandler ) h;
      }

      // This is a must override method.
      public virtual void ReleaseHandler ( IHttpHandler handler ) 
      {
      }
   }
   
   // Class definition for abc.aspx handler.
   public class abc : IHttpHandler
   {
      public virtual void ProcessRequest ( HttpContext context ) 
      {
         context.Response.Write ( "<html><body>" );
         context.Response.Write ( "<p>ABC Handler</p>\n" );
         context.Response.Write ( "</body></html>" );
      }
    
      public virtual bool IsReusable
      {
         get { return true; }
      }
   }

   // Class definition for xyz.aspx handler.
   public class xyz : IHttpHandler
   {
      public virtual void ProcessRequest ( HttpContext context ) 
      {
         context.Response.Write ( "<html><body>" );
         context.Response.Write ( "<p>XYZ Handler</p>\n" );
         context.Response.Write ( "</body></html>" );
      }
    
      public virtual bool IsReusable
      {
         get { return true; }
      }
   }
}

To use the above handler factory, add the following lines to the Web.config file.

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="abc.aspx" type="test.myFactory,HandlerFactoryTest" />
         <add verb="*" path="xyz.aspx" type="test.myFactory,HandlerFactoryTest" />
      </httpHandlers>     
   </system.web>
</configuration>
See Also

IHttpHandlerFactory Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph