IHttpHandlerFactory.GetHandler(HttpContext, String, String, String) 메서드

정의

IHttpHandler 인터페이스를 구현하는 클래스의 인스턴스를 반환합니다.

public:
 System::Web::IHttpHandler ^ GetHandler(System::Web::HttpContext ^ context, System::String ^ requestType, System::String ^ url, System::String ^ pathTranslated);
public System.Web.IHttpHandler GetHandler (System.Web.HttpContext context, string requestType, string url, string pathTranslated);
abstract member GetHandler : System.Web.HttpContext * string * string * string -> System.Web.IHttpHandler
Public Function GetHandler (context As HttpContext, requestType As String, url As String, pathTranslated As String) As IHttpHandler

매개 변수

context
HttpContext

내장 서버 개체(예: Request, Response, SessionServer)에 대한 참조를 제공하는 HttpContext 클래스의 인스턴스는 HTTP 요청을 지원하는 데 사용됩니다.

requestType
String

클라이언트에서 사용하는 HTTP 데이터 전송 메서드(GET 또는 POST)입니다.

url
String

요청된 리소스의 RawUrl입니다.

pathTranslated
String

요청된 리소스에 대한 PhysicalApplicationPath입니다.

반환

IHttpHandler

요청을 처리하는 새 IHttpHandler 개체입니다.

예제

다음 코드 예제에는 클라이언트 요청에 대 한 응답으로 사용자 지정 처리기 개체를 만드는 방법을 보여 줍니다. 이 예제는 두 부분으로 구성 합니다.

  • 처리기 팩터리 클래스입니다.

  • Web.config 파일 발췌입니다.

예제의 첫 번째 부분에서는 이름이 abc.aspx 또는 xyz.aspx 인 라는 페이지에 대 한 클라이언트 요청에 대 한 응답에서 사용자 지정 처리기 개체를 만드는 방법을 보여 줍니다. 명명 된 처리기 팩터리 클래스 hwf 요청한 페이지에 따라 적절 한 처리기 개체를 만듭니다.

// 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
   {
      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
      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 handler factory, use the following lines in a
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>
*/
' 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.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
        
        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        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

'______________________________________________________________
'
'To use the handler factory, use the following lines in a
'Web.config file. (be sure to remove the comment markers)
'
'         <add verb="*" path="abc.aspx" type="test.MyFactory,HandlerFactoryTest" />
'         <add verb="*" path="xyz.aspx" type="test.MyFactory,HandlerFactoryTest" />

예제의 두 번째 부분에서는 Web.config 파일 발췌를 보여 줍니다. 위의 처리기 팩터리를 사용 하려면 Web.config 파일에 다음 줄을 추가 합니다.

<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>   

적용 대상