一般处理程序
- 定义一个html页面
index.htlm
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<td>username:</td>
<td>$username</td>
</tr>
<tr>
<td>password:</td>
<td>$pwd</td>
</tr>
</table>
</body>
</html>
- 新建一个一般处理程序
.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string filePath= context.Request.MapPath("indexH.html");//获取物理路径(在web开发中,对文件操作的时候,必须先要获取物理路径)
string strHtml = File.ReadAllText(filePath); //读取文件内容
strHtml = strHtml.Replace("$username","fxx").Replace("$pwd","123"); //替换指定html里的占位符
context.Response.Write(strHtml);//输出替换好的页面
}
public bool IsReusable {
get {
return false;
}
}
}
-
运行
image.png
一般程序接收POST表单
- 表单页面
htmlPost
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="AcceptInfo.ashx" method="post">
USERNAME:<input type="type" name="un1" value="" />
PASSWORD:<input type="type" name="pwd1" value="" />
<input type="submit" value="submit" />
</form>
</body>
</html>
- 接收数据页面
AcceptInfo.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string un1 = context.Request.Form["un1"];
string pwd1 = context.Request.Form["pwd1"];//标点如果是以post方式提交过来的,接受时必须用Request.Form来收,且表单元素必须有name属性
context.Response.Write("用户名是"+un1+";密码是:"+pwd1);
}
public bool IsReusable {
get {
return false;
}
}
}
一般程序接收get请求
将context.Request.Form
改成context.Request.QueryString
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string un1 = context.Request.QueryString["un1"];
string pwd1 = context.Request.QueryString["pwd1"];//标点如果是以post方式提交过来的,接受时必须用Request.Form来收,且表单元素必须有name属性
context.Response.Write("用户名是"+un1+";密码是:"+pwd1);
}
public bool IsReusable {
get {
return false;
}
}
}
表单练习
- 设置数据库
web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<!--sql配置-->
<connectionStrings>
<add name="connStr" connectionString="server=127.0.0.1;uid=sa;pwd=shangxi;database=new_test"/>
</connectionStrings>
</configuration>
- 页面
userInfoList.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table>
<tr>
<th>编号</th><th>时间</th><th>排名</th><th>交易金额</th>
</tr>
$tbody
</table>
</body>
</html>
- 数据库读数据
UserInfoLish.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string conntring = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(conntring))
{
using(SqlDataAdapter apter = new SqlDataAdapter("select Id,businessDate,orderNum,tradeMoney from oprPVshop order by orderNum ", conn))
{
DataTable da = new DataTable();
apter.Fill(da);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < da.Rows.Count; i++)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", da.Rows[i]["Id"], Convert.ToString(da.Rows[i]["businessDate"]).Substring(0,9), da.Rows[i]["orderNum"], da.Rows[i]["tradeMoney"]);
}
string filePathe = context.Request.MapPath("UserInfoList.html");
string fileContent = File.ReadAllText(filePathe);
fileContent = fileContent.Replace("$tbody", sb.ToString());
context.Response.Write(fileContent);
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
image.png
添加用户
- 添加用户表单页面:
addUserinfo.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="addUserInfo.ashx" method="post">
<table>
<tr>
<td>时间</td>
<td><input type="text" name="addTime" value="" /></td>
</tr>
<tr>
<td>排名</td>
<td><input type="text" name="addNum" value="" /></td>
</tr>
<tr>
<td>交易金额</td>
<td><input type="text" name="addMoney" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
</body>
</html>
- 数据库处理页面:
addUserInfo.ashx
<%@ WebHandler Language="C#" Class="addUserInfo" %>
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class addUserInfo : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string conntring = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(conntring))
{
using(SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "insert into oprPVshop(businessDate,orderNum,tradeMoney)values(@businessDate,@orderNum,@tradeMoney)";
cmd.Parameters.Add("@businessDate", context.Request.Form["addTime"]);
cmd.Parameters.Add("@orderNum", context.Request.Form["addNum"]);
cmd.Parameters.Add("@tradeMoney", context.Request.Form["addMoney"]);
if (cmd.ExecuteNonQuery()>0)
{
context.Response.Redirect("UserInfoList.ashx");
}
else
{
context.Response.Write("添加失败");
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
网友评论