IHttpHandler.ProcessRequest(HttpContext) 方法


通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。Enables processing of HTTP Web requests by a custom HttpHandler that implements the IHttpHandler interface.

 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)



HttpContext 对象,该对象提供对用于为 HTTP 请求提供服务的内部服务器对象(如 RequestResponseSessionServer)的引用。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 的所有请求均由程序集MyHttpHandler HandlerTest 中包含的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.

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

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