asp.net 请求request,处理handler,响应response

来源:互联网 发布:主机网络ip地址怎么查 编辑:程序博客网 时间:2024/05/16 14:29

HTMLpage2.htm页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title></head><body><form action="Handler2.ashx"><input type="hidden" name="ispostback" value="true" />姓名:<input type="text" name="number" value ="@value" /> <input type="submit" value="提交" /></form></body></html>


Handler2.ashx页

 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;namespace ASP.NET初级{    /// <summary>    /// Handler1 的摘要说明    /// </summary>    public class Handler2 : IHttpHandler    {        //当一个请求发过来的时候,就会触发ProcessRequest(HttpContext context)方法          public void ProcessRequest(HttpContext context)        {            //context.Response.ContentType = "text/html";            //Server.MapPath的作用是:返回与web服务器上指定虚拟路径相对应的物理物理文件路径            //string path = context.Server.MapPath("~/HTMLpage2.htm");//取得HTMLpage2.htm文件的全路径。例如:D:\学习资料\ASP.NET20140801\ASP.NET初级\HTMLpage2.html            //string contex = File.ReadAllText(path);//读取文件(也就是加载模板)              //context.Response.Write(contex);//再把读取到的文件内容打印出来              /*              string username =context.Request["UserName"]; //取得提交过来的name值(UserName)              if (string.IsNullOrEmpty(username))//如果username为空              {                  context.Response.Write("直接进入");              }              else              {                  context.Response.Write("提交进入");              }              */            //----------------------------------------------------------------------------            context.Response.ContentType = "text/html";            string ispostback = context.Request["ispostback"];            string number = context.Request["number"];//通过表单得到的数据都是string类型              if (ispostback == "true")//如果ispostback有值,那么也就是说HTMLpage2.htm页通过提交传递了一个name="ispostback"的value值            {                //context.Response.Write("提交进入");                  int i = Convert.ToInt32(number);                i++;                number = i.ToString();            }            else            {                //context.Response.Write("直接进入");                  number = "0";            }            string fullpath = context.Server.MapPath("HTMLPage2.htm");            string con = File.ReadAllText(fullpath);//加载模板              con = con.Replace("@value", number);//将模板中占位符替换为值              context.Response.Write(con);    //响应阶段,把读取到并经过处理的文件打印出来。          }        public bool IsReusable        {            get            {                return false;            }        }    }    /*     <1>.Server.MapPath()介绍     Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文件路径。其参数path为Web 服务器的虚拟路径,返回结果是与path相对应的物理文件路径。但有时参数并非为虚拟路径,而是用户自定义的文件名。     Server.MapPath()的全名是System.Web.HttpContext.Current.Server.MapPath()。有时在程序调试时会提示“当前上下文中不存在名称“Server””错误,从而不支持函数Server.MapPath()的使用。尽管引用了命名空间“using System.Web;”也是无济于事,此时就需要使用其全名,或者是当前使用Server.MapPath()函数的类继承自System.Web.UI.Page。    <2>.Server.MapPath()应用  Server.MapPath("") :返回当前页面所在的物理文件路径  Server.MapPath("/") :返回应用程序根目录所在的物理文件路径  Server.MapPath("./") :返回当前页面所在的物理文件路径  Server.MapPath("../"):返回当前页面所在的上一级的物理文件路径  Server.MapPath("~/"):返回应用程序的虚拟目录(路径)  Server.MapPath("~"):返回应用程序的虚拟目录(路径)  3.      说明:对于Server.MapPath()具体返回什么内容,在不同的环境下得到的结果也许并不相同。正如上面的六个Server.MapPath()应用,我在VS2010中测试时,其中第二项、第四项会提示错误“未能映射路径”,但是改变程序所在的位置时,仅第二项会提示错误。故不同的软件、不同的环境对其支持度也不同。     */    /*     ReadAllText(File, Encoding)     File:     要读取的文件的文件名和路径     Encoding: 可选参数,Encoding类型,可选的编码有:Default;UTF8;UTF7; Unicode ;BigEndianUnicode等等。Default表示系统编码,对于中文操作系统,通常是GB2312     * 一般不需要指定文本文件的编码,ReadAllText会尝试自动检查。     * 但是如果读取的文本文件出现乱码,那么应该考虑明确指定文件编码,例如:string content = FileSys.ReadAllText("c:\data\table.txt", Encoding.Default)          */}


实现一个加法计算器的功能(它有两个页面,一个html页面,一个handler页面)

html也作为模板,是不给用户看的。经过处理后的handler页面才是呈现给用户看的

(因为在WebApplication里不能将handler也设为启动项,调试的时候就直接在浏览器中输入http://localhost:32451/Handler1.ashx)就行了

HTMLPage1.htm页

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script type="text/javascript">        function clearVal3() {            document.getElementById("t1").onchange=function () {                document.getElementById("t3").value = "";            }        }    </script></head><body onload="clearVal3()">    <form action="Handler1.ashx" method="get">        <input type="hidden" name="ispostback" value="true" />        <input type="text" value="@value1" name="txt1" id="t1"/>+        <input type="text" value="@value2" name="txt2" />=        <input type="text" value="@value3" name="txt3" id="t3"/>        <input type="submit"  value="提交"/>    </form></body></html>


Handler1.ashx

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace ASP.NET初级{    /// <summary>    /// Handler1 的摘要说明    /// </summary>    public class Handler1 : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/html";            string ispostback= context.Request["ispostback"];            string path = context.Server.MapPath("HTMLPage1.htm");            string val1 = context.Request["txt1"];            string val2 = context.Request["txt2"];                        string val3 = (Convert.ToInt32(val1) + Convert.ToInt32(val2)).ToString();            string readpath = System.IO.File.ReadAllText(path);            if (ispostback == "true")  //如果通过提交进入页面的那么text文本框肯定是已经填写值的了。然后获取文本框的值,替换原来的@value1,2,3值就好了            {                readpath = readpath.Replace("@value1", val1);                readpath = readpath.Replace("@value2", val2);                readpath = readpath.Replace("@value3", val3);                            }            else   //如果直接进入页面,也就是不是通过提交进入的。就将text文本框的值都设为""空值            {                               readpath = readpath.Replace("@value1", "");                readpath = readpath.Replace("@value2", "");                readpath = readpath.Replace("@value3", "");                            }            context.Response.Write(readpath);   //将已经处理的字符串readpath(readpath字符串其实就是html代码) 进行输出,然后浏览器解析这段字符串,将他显示在页面上。            }        public bool IsReusable        {            get            {                return false;            }        }    }}