标签:style blog http color 使用 os io 文件
以下就是使用一般处理程序完成的计算器加法,其他运算符也可以按此思路完成。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Text; 6 7 namespace _02加法计算器 8 { 9 /// <summary> 10 /// CalcAdd 的摘要说明 11 /// </summary> 12 public class CalcAdd : IHttpHandler 13 { 14 15 public void ProcessRequest(HttpContext context) 16 { 17 context.Response.ContentType = "text/html"; 18 StringBuilder sb = new StringBuilder(); 19 int num1 = 0; 20 int num2 = 0; 21 int result = 0; 22 if (context.Request["num1"] != null) 23 { 24 num1 = int.Parse(context.Request["num1"]); 25 num2 = int.Parse(context.Request["num2"]); 26 result = num1 + num2; 27 } 28 sb.AppendLine("<form method=‘get‘>"); 29 sb.AppendLine("<input type=‘text‘ name=‘num1‘ value=‘" + num1 + "‘>+"); 30 sb.AppendLine("<input type=‘text‘ name=‘num2‘ value=‘" + num2 + "‘>"); 31 sb.AppendLine("<input type=‘submit‘ value=‘=‘>"); 32 sb.AppendLine("<input type=‘text‘ name=‘result‘ value=‘" + result + "‘>"); 33 sb.AppendLine("</form>"); 34 35 36 context.Response.Write(sb.ToString()); 37 } 38 39 public bool IsReusable 40 { 41 get 42 { 43 return false; 44 } 45 } 46 } 47 }
请求后:
细心的朋友会发现地址栏 result 是等于0的,然而在文本框中的值是2
原因是当你第一次提交的时候 result 文本框中的值是0,然后提交到了服务器中,然后服务器在响应回发给你的时候就已经把 result 中的值重新赋值了
下面就开始使用模板页来完成计算器加法
首先要准备一个模板页(静态文件)
CalcAdd.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title></title> 6 </head> 7 <body> 8 <form method="get"> 9 <input type="text" name="num1" value="$num1" /> 10 + 11 <input type="text" name="num2" value="$num2" /> 12 <input type="submit" value="=" /> 13 <input type="text" name="result" value="$result" /> 14 </form> 15 </body> 16 </html>
浏览器中显示的是:
OK,我们准备好了模板页后就开始如下的操作:
1.新建一个一般处理程序
2.处理请求(如果是第一次响应则输出模板页,如果是回发请求就接收值后经过处理后输出)
3.读取模板页
4.替换模板页中的值
5.响应请求
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Text; 6 using System.IO; 7 8 namespace _01加法计算器 9 { 10 /// <summary> 11 /// CalcAdd2 的摘要说明 12 /// </summary> 13 public class CalcAdd2 : IHttpHandler 14 { 15 16 public void ProcessRequest(HttpContext context) 17 { 18 context.Response.ContentType = "text/html"; 19 int num1 = 0, num2 = 0, result = 0; 20 if (context.Request["num1"] != null) 21 { 22 num1 = int.Parse(context.Request["num1"]); 23 num2 = int.Parse(context.Request["num2"]); 24 result = num1 + num2; 25 } 26 string html = File.ReadAllText 27 ( 28 Path.Combine 29 ( 30 //获得文件夹 31 AppDomain.CurrentDomain.BaseDirectory 32 , 33 //模板页 34 "CalcAdd.html" 35 ) 36 ); 37 //替换值 38 html = html.Replace("$num1", num1.ToString()) 39 .Replace("$num2", num2.ToString()) 40 .Replace("$result", result.ToString()); 41 context.Response.Write(html); 42 } 43 44 public bool IsReusable 45 { 46 get 47 { 48 return false; 49 } 50 } 51 } 52 }
请求后:
使用一般处理程序完成计算器加法运算(并使用模板页),布布扣,bubuko.com
标签:style blog http color 使用 os io 文件
原文地址:http://www.cnblogs.com/bujingkua/p/3917972.html