1.第一个程序
目录文件:
表格(非html文件):
<%@ WebHandler Language="C#" Class="First" %>
using System;
using System.Text;
using System.Web;
public class First : IHttpHandler
{
//请求过来找到该一般处理程序文件,自动执行ProcessRequest方法。
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";//指定返回给浏览器的数据类型是文本字符串
//context.Response.Write("Hello World");//将字符串返回给浏览器。
StringBuilder sb = new StringBuilder();
sb.Append("<table border='1'><tr><td>用户名</td><td>itcast</td></tr>");
sb.Append("<tr><td>密码</td><td>123</td></tr></table>");
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
第一个程序
</body>
</html>
获取数据文件:
<%@ WebHandler Language="C#" Class="Show" %>
using System;
using System.IO;
using System.Web;
public class Show : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//获取要操作的模板路径
//获取文件的物理路径。
string path = context.Request.MapPath("ShowInfo.html");
//读取模板文件中的内容
string fileContent = File.ReadAllText(path);
//用户具体的数据替换模板文件中的占位符
fileContent = fileContent.Replace("$name", "tsp").Replace("pwd","123");
//将替换后的内容输出给浏览器
context.Response.Write(fileContent);
context.Response.Write("<b>saadasdasd</b>");
}
public bool IsReusable
{
get
{
return false;
}
}
}
html文件:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
.txt {
font-size: 14px;
color: red;
}
</style>
</head>
<body>
<table>
<tr>
<td>用户名</td>
<td class="txt">$name</td>
</tr>
<tr>
<td>密码</td>
<td class="txt">$ped</td>
</tr>
</table>
</body>
</html>
2.请求方法
html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!---表单:收集用户数据-->
<form method="post" action="AddInfo.ashx">
用户名:<input type="text" name="txtName" /><br />
密  码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
添加结果:
<%@ WebHandler Language="C#" Class="AddInfo" %>
using System;
using System.Web;
public class AddInfo : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//string userName = context.Request.QueryString["txtName"];
//string userPwd = context.Request.QueryString["txtPwd"];
string userName = context.Request.Form["txtName"];
string userPwd = context.Request.Form["txtPwd"];
context.Response.Write("用户名是:" + userName);
context.Response.Write("密码是:" + userPwd);
}
public bool IsReusable
{
get
{
return false;
}
}
}
获取文件:
<%@ WebHandler Language="C#" Class="ShowAdd" %>
using System;
using System.IO;
using System.Web;
public class ShowAdd : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//读取模板文件
string filePath = context.Request.MapPath("Add.html");
string fileContent = File.ReadAllText(filePath);
context.Response.Write(fileContent);
}
public bool IsReusable {
get {
return false;
}
}
}