IHttpHandler.ProcessRequest(HttpContext) IHttpHandler.ProcessRequest(HttpContext) IHttpHandler.ProcessRequest(HttpContext) IHttpHandler.ProcessRequest(HttpContext) Method

定義

以實作 IHttpHandler 介面的自訂 HttpHandler 來啟用 HTTP Web 要求的處理。Enables processing of HTTP Web requests by a custom HttpHandler that implements the IHttpHandler interface.

public:
 void ProcessRequest(System::Web::HttpContext ^ context);
public void ProcessRequest (System.Web.HttpContext context);
abstract member ProcessRequest : System.Web.HttpContext -> unit
Public Sub ProcessRequest (context As HttpContext)

參數

context
HttpContext HttpContext HttpContext HttpContext

HttpContext 物件,提供對內建伺服器物件 (例如 RequestResponseSessionServer) 的參考,用來服務 HTTP 要求。An HttpContext object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.

範例

下列程式碼範例會將四行文字寫入 HTTP 輸出資料流程, 以回應名為 handler 的頁面的用戶端要求。The following code example writes four lines of text to the HTTP output stream in response to a client request for a page named handler.aspx. 處理常式 .aspx 的所有要求都是由 HandlerTest MyHttpHandler元件所包含之HandlerExample命名空間中的類別提供服務。All requests for handler.aspx are serviced by the MyHttpHandler class in the namespace HandlerExample contained in the assembly HandlerTest.dll.

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

using System.Web;

namespace HandlerExample
{
   public class MyHttpHandler : IHttpHandler
   {
      // Override the ProcessRequest method.
      public void ProcessRequest(HttpContext context)
      {
         context.Response.Write("<H1>This is an HttpHandler Test.</H1>");      
         context.Response.Write("<p>Your Browser:</p>");
         context.Response.Write("Type: " + context.Request.Browser.Type + "<br>");
         context.Response.Write("Version: " + context.Request.Browser.Version);
      }

      // Override the IsReusable property.
      public bool IsReusable
      {
         get { return true; }
      }
   }
}

/*
______________________________________________________________

To use this handler, include the following lines in a Web.config file.

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="handler.aspx" type="HandlerExample.MyHttpHandler,HandlerTest"/>
      </httpHandlers>
   </system.web>
</configuration>
*/

' Name this Visual Basic file HandlerTest.vb and compile it with the
' command line: vbc /target:library /r:System.Web.dll HandlerTest.vb.
' Copy HandlerTest.dll to your \bin directory.
Imports System.Web

Namespace HandlerExample
    
    Public Class MyHttpHandler
        Implements IHttpHandler
        
        ' Override the ProcessRequest method.
        Public Sub ProcessRequest(context As HttpContext) _
        Implements IHttpHandler.ProcessRequest
        
            context.Response.Write("<H1>This is an HttpHandler Test.</H1>")
            context.Response.Write("<p>Your Browser:</p>")
            context.Response.Write("Type: " & context.Request.Browser.Type & "<br>")
            context.Response.Write("Version: " & context.Request.Browser.Version)
        End Sub
        
        ' Override the IsReusable property.        
        Public ReadOnly Property IsReusable() As Boolean _
        Implements IHttpHandler.IsReusable
        
            Get
                Return True
            End Get
        End Property
    End Class
End Namespace

'______________________________________________________________
'
' To use this handler, include the following lines in a
' Web.config file (be sure to remove the comment markers).
'
'         <add verb="*" path="handler.aspx" type="HandlerExample.MyHttpHandler,HandlerTest"/>

備註

將您的HttpHandler自訂程式ProcessRequest代碼放在虛擬方法中, 如範例一節所示。Place your custom HttpHandler code in the ProcessRequest virtual method, as shown in the Examples section.

適用於