The following is a calculator addition completed by a general processing program. Other operators can also be completed in this way.
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. web; 5 using system. text; 6 7 namespace _ 02 addition calculator 8 {9 /// <summary> 10 /// Summary of calcadd 11 /// </Summary> 12 public class calcadd: ihttphandler13 {14 15 public void processrequest (httpcontext context) 16 {17 context. response. contenttype = "text/html"; 18 stringbuilder sb = new stringbuilder (); 19 int num1 = 0; 20 in T 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 isreusable40 {41 get42 {43 return false; 44} 45} 46} 47}
After request:
Careful friends will find that the address bar result is equal to 0, but the value in the text box is 2
The reason is that when you submit the result for the first time, the value in the result text box is 0, and then submitted to the server. Then, when the server returns the result to you, the value in the result is assigned a new value.
Next we will start to use the template page to complete the calculator addition.
First, prepare a template page (static file)
Calcadd.html
1 <!DOCTYPE html> 2
The following information is displayed in the browser:
OK. After preparing the template page, we will start the following operations:
1. Create a general processing program
2. process the request (if it is the first response, the template page will be output. If it is a send-back request, the value will be received and then output after processing)
3. Read the template page
4. Replace the values on the template page
5. Response Request
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 addition calculator 9 {10 /// <summary> 11 /// Summary of calcadd2 12 /// </Summary> 13 public class calcadd2: ihttphandler14 {15 16 public void processrequest (httpcontext context) 17 {18 context. response. contenttype = "text/html"; 19 int num1 = 0, num2 = 0, result = 0; 20 I F (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. readalltext27 (28 path. combine29 (30 // get the folder 31 appdomain. currentdomain. basedirectory32, 33 // template page 34 "calcadd.html" 35) 36); 37 // replace value 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 isreusable45 {46 get47 {48 return false; 49} 50} 51} 52}
After request: