/WebSiteTest中新建
AddCalc1.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="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="@num1" />+<input type="text" name="num2" value="@num2" /><input type="submit" value="=" />
<input type="text" value="@result" readonly="readonly" />
</form>
</body>
</html>
-------------------------------------------------------
<%@ WebHandler Language="C#" Class="AddCalc1" %>
using System;
using System.Web;
public class AddCalc1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string huilaila = context.Request["huilaile"];
// context.Response.Write("Hello World");
//如果想保存,前两个加数的值,需要添加变量
string num1 = "";
string num2 = "";
string result = "";
if (huilaila=="yes") {
num1=context.Request["num1"];
num2=context.Request["num2"];
result = Convert.ToString (Convert.ToInt32(num1) + Convert.ToInt32(num2));
context.Response.Write("<script type='text/javascript'>alert('Hello everyOne Im Credream')</script>");
}
string html文件模板全路径 = context.Server.MapPath("AddCalc1.htm");//得到文件的全路径
string content = System.IO.File.ReadAllText(html文件模板全路径);
//替换前两个单元格的值,保存原有的值
//这里每写一次:content就是一个html页面,但是由于浏览器的容错性,所以不会显示出错
// context.Response.Write(content);
//这里的context.Response.Write(content);,可以看出来每一步,它是如何替换的
content = content.Replace("@num1", num1);
// context.Response.Write(content);
content = content.Replace("@num2", num2);
// context.Response.Write(content);
content = content.Replace("@result", result);
context.Response.Write(content);
}
public bool IsReusable {
get {
return false;
}
}
}
-------------------------------
生成的下面的html代码很明显是错误的,但是显示出来并没有问题,提现了浏览器的容错性:
先看看浏览器的容错性:
<script type='text/javascript'>alert('Hello everyOne Im Credream')</script><!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="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="1" />+<input type="text" name="num2" value="1" /><input type="submit" value="=" />
<input type="text" value="2" readonly="readonly" />
</form>
</body>
</html>
AddCalc1.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="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="@num1" />+<input type="text" name="num2" value="@num2" /><input type="submit" value="=" />
<input type="text" value="@result" readonly="readonly" />
</form>
</body>
</html>
-------------------------------------------------------
<%@ WebHandler Language="C#" Class="AddCalc1" %>
using System;
using System.Web;
public class AddCalc1 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string huilaila = context.Request["huilaile"];
// context.Response.Write("Hello World");
//如果想保存,前两个加数的值,需要添加变量
string num1 = "";
string num2 = "";
string result = "";
if (huilaila=="yes") {
num1=context.Request["num1"];
num2=context.Request["num2"];
result = Convert.ToString (Convert.ToInt32(num1) + Convert.ToInt32(num2));
context.Response.Write("<script type='text/javascript'>alert('Hello everyOne Im Credream')</script>");
}
string html文件模板全路径 = context.Server.MapPath("AddCalc1.htm");//得到文件的全路径
string content = System.IO.File.ReadAllText(html文件模板全路径);
//替换前两个单元格的值,保存原有的值
//这里每写一次:content就是一个html页面,但是由于浏览器的容错性,所以不会显示出错
// context.Response.Write(content);
//这里的context.Response.Write(content);,可以看出来每一步,它是如何替换的
content = content.Replace("@num1", num1);
// context.Response.Write(content);
content = content.Replace("@num2", num2);
// context.Response.Write(content);
content = content.Replace("@result", result);
context.Response.Write(content);
}
public bool IsReusable {
get {
return false;
}
}
}
-------------------------------
生成的下面的html代码很明显是错误的,但是显示出来并没有问题,提现了浏览器的容错性:
先看看浏览器的容错性:
<script type='text/javascript'>alert('Hello everyOne Im Credream')</script><!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="AddCalc1.ashx">
<input type="hidden" name="huilaile" value="yes" />
<input type="text" name="num1" value="1" />+<input type="text" name="num2" value="1" /><input type="submit" value="=" />
<input type="text" value="2" readonly="readonly" />
</form>
</body>
</html>