首页 > 编程 > .NET > 正文

.NET之ASP WebApplication快速入门(5)(转载)

2024-07-10 12:58:17
字体:
来源:转载
供稿:网友
创建自定义httphandler


要自定义一个handler,可以执行ihttphandler,并在config.web文件
的httphandlers一节中添加类信息。下面举例说明如何创建自定义一个
httphandler,将所有的请求对应到"simplehandler.aspx"中:

simplehandler


[]点击运行程序]
| []查看源代码]

自定义httphandler可以通过执行ihttphandler接口来创建,这个接口
只有两个方法。通过调用isreusable,一个http
factory就能够查询handler(处理器)以判断是否同一实例可以用于服
务多个请求。processrequest方法接受httpcontext实例作为参数。这
里的例子中,请求数据被忽略,一个常量字符串作为响应发送到客户端。
请看下面使用vb、c#以及jscript三种语言编写的代码:

c#


public class simplehandler : ihttphandler {
  public void processrequest(httpcontext context) {
    context.response.write("hello world!");
  }

  public bool isreusable() {
    return true;
  }
}

vb

public class simplehandler : inherits ihttphandler
  public sub processrequest(context as httpcontext)
    context.response.write("hello world!")
  end sub

  public function isreusable() as boolean
    return(true)
  end function
end class

jscript

public class simplehandler implements ihttphandler {
  public function processrequest(context:httpcontext) :
void {
    context.response.write("hello world!");
  }

  public function isreusable() : boolean {
    return true;
  }
}

将编译的处理器集合放置到application的/bin目录下面后,我们就可以
指定处理器类到请求的目标上。在这里,所有
对"simplehandler.aspx"的请求将被路由到simplehandler类的一个实
例上,它生存于名字空间acme.simplehandler中。

结 语


以上通过原理与实例讲述了.net之asp web application的概念及使
用,我们看到了如何使用三种不同的编程语言达到目的。你会发现,我们
正在慢慢地触及.net的神奇思想,领会.net的震撼力量。我们有理由相
信,凭借如此强大的工具,开发人员将更具创造力!

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表