1、一般处理程序是什么?

    答:一般处理程序是以.ashx结尾的文件,默认命名为Handler1.ashx。

          用在Web项目中,也就是我们常说的网站项目。

2、新建一个一般处理程序

    1.1 新建一个空网站

    1.2 在新建的空网站中添加新建一般处理程序,命名为TestHandler.ashx

          可以看到代码为:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace TestHandler
 7 {
 8     /// <summary>
 9     /// TestHandler 的摘要说明
10     /// </summary>
11     public class TestHandler : IHttpHandler
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             context.Response.ContentType = "text/plain";
17             context.Response.Write("Hello World");
18         }
19 
20         public bool IsReusable
21         {
22             get
23             {
24                 return false;
25             }
26         }
27     }
28 }

 

    1.3 运行一般处理程序

     F5运行时,会发现浏览器显示了一个列表,因为ashx不是浏览器认识的可以展示的内容,需要在后面指定运行的一般处理程序名称,或者,直接在列表中选择你要查看运行的ashx.

          

         运行显示结果:是1.2中输出的"Hello World"。 此时浏览器的地址是:http://localhost:14242/TestHandler.ashx(本地)

    1.4 参数设置 参数获取 

          参数设置:在浏览器地址后面添加参数,如http://localhost:14242/TestHandler.ashx?name=zhangsan&age=18

         参数获取:在后台代码中该如何获取呢,如下

 1 public class TestHandler : IHttpHandler
 2     {
 3 
 4         public void ProcessRequest(HttpContext context)
 5         {
 6             context.Response.ContentType = "text/html";
 7             string name = context.Request["name"];
 8             string age = context.Request["age"];
 9             context.Response.Write("Hello My name is "+name+" and I'm "+age);
10         }
11 
12         public bool IsReusable
13         {
14             get
15             {
16                 return false;
17             }
18         }
19     }

     运行结果是:Hello My name is zhangsan and I'm 18

     当然正式程序中肯定不会用这种方式设置参数,这只是初步理解一般处理程序

3、表单提交到一般处理程序

  3.1 新建一个HTML文件,编写form表单,代码如下

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7 <form action="TestHandler.ashx" method="post">
 8     姓名:<input type="text" name="username"/>
 9     年龄:<input type="text" name="age"/>
10     <input type="submit" />
11 </form>
12 </body>
13 </html>

      3.2 name的属性就是传递给一般处理程序接收的

      3.3

4、XXX

posted on 2015-07-23 17:53  Lacey.zhao  阅读(725)  评论(0编辑  收藏  举报