IHttpHandler.ProcessRequest(HttpContext) 方法

定义

通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。

public void ProcessRequest(System.Web.HttpContext context);

参数

context
HttpContext

HttpContext 对象,该对象提供对用于为 HTTP 请求提供服务的内部服务器对象(如 RequestResponseSessionServer)的引用。

示例

下面的代码示例将四行文本写入 HTTP 输出流,以响应客户端对名为 handler.aspx 的页面的请求。 handler.aspx 的所有请求都由 MyHttpHandler 程序集HandlerTest.dll中包含的命名空间 HandlerExample 中的 类提供服务。

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

注解

将自定义 HttpHandler 代码放入虚拟方法中 ProcessRequest ,如示例部分所示。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1