get post请求
1.首先建个文件夹 将建的东西都放在一个文件夹下
(1)建html
<!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 method="get" action="AddInfo.ashx">
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
(2)建个一般处理程序
<%@ WebHandler Language="C#" Class="ShowAdd" %>
using System;
using System.Web;
using System.IO;
public class ShowAdd : IHttpHandler {
//用户先访问showAdd.ashx这个一般处理程序 一般处理程序读取模板文件html 然后把读取后的内容返回给浏览器
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);
//收集完数据,用户点按钮把输入的数据以get方式发送到AddInfo.ashx这个页面
//AddInfo这个一般处理程序开始发送一个get请求,并且把表单里的数据传到AddInfo里面 Add和AddInfo要在同一文件夹下
}
public bool IsReusable {
get {
return false;
}
}
}
(3)在建一个一般处理程序
<%@ WebHandler Language="C#" Class="AddInfo" %>
using System;
using System.Web;
public class AddInfo : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain"; //Response输出
//get方式
//string username = context.Request.QueryString["txtName"];//接收的是表单元素name的值 Request接收的意思
//string userPwd= context.Request.QueryString["txtPwd"];
//post方式
string username = context.Request.Form["txtName"];
string userPwd = context.Request.Form["txtPwd"];
context.Response.Write("用户名是:"+username+",密码是:"+userPwd);
//传的方式不同 get方式是把数据放在地址栏里的,post方式是把数据放在请求报文里的
}
public bool IsReusable {
get {
return false;
}
}
}
总结内容
学asp.net本质论 get post 后面会经常用,暂时理解一下,不需要刻意记