IHttpHandler 系列一 入门篇

来源:互联网 发布:简单相册制作软件 编辑:程序博客网 时间:2024/06/02 16:12

using System.Web;

namespace Phone.HttpHandler
{
 public class SimpleHandler : IHttpHandler
 {
  // Override the ProcessRequest method
  public void ProcessRequest(HttpContext context)
  {
   //context.Response.Write("<H1>Hello, I'm an HTTP handler</H1>");
   if( context.Request.UrlReferrer != null)
   {
    context.Response.Write("upurl:" + context.Request.UrlReferrer.ToString());
   }
   string _ip = context.Request.UserHostAddress;   //获取IP
   if(_ip=="")
   {
    context.Response.Write("对不起,您的IP:"+_ip+"已被限制!");
    context.Response.End();
   }
  }
  // Override the IsReusable property
  public bool IsReusable
  {
   get { return true; }
  }
 }
}

以上就是一个简单的实现了IHttpHandler接口的处理程序,调用该程序的web 项目要添加该项目的引用,并在weg.config中加入如下配置:

<httpHandlers>
   <add verb="POST,GET" path="*.aspxh" type="Phone.HttpHandler.SimpleHandler,Phone.HttpHandler" />
 </httpHandlers>

verb处理的动作, path处理的文件类型,type=带命名空间的类路径,命名空间

注意一个web.config中只能有一个<httpHandlers>节点,不然会报错,但你可以在节点中添中多条记录