ASP.NET前端和后台交互基本过程测试
1、 --》新建项目 --》ASP.NET 空web程序
2、 --》新建HTML文件
3、分别新建 DAL (数据访问层)文件夹 Handler 文件夹
4、在 Handler 文件夹中添加 一般处理程序 并命名 LoginHandler
5、在 DAL 文件夹中 添加 AdminService类
6、设置 UserLogin.html 为起始页
7、启动程序运行 测试结果
HTML 长成这个样子得
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>用户登录</title>
<meta charset="utf-8" />
</head>
<body>
<form action="Handler/LoginHandler1.ashx" method="post">
用户名 : <input type="text" name="uname" value=" " />
密码 :<input type="password" name="upwd" value=" " />
<input type="submit" name="btnSubmit" value="登录" />
</form>
</body>
</html>
AdminService
public bool AdminLogin(string name ,string pwd)
{
//模拟数据库判断
if (name=="admin"&&pwd=="123456")
{
return true;
}
else
{
return false;
}
LoginHanler
using ASP.NETDemo.DAL; //引入命名空间
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//获取前台网页所提交得数据
string uname = context.Request.Params["uname"];
string upwd = context.Request.Params["upwd"];
//调用数据访问方法的判断用户名与密码是否正确
AdminService objservice = new AdminService();
if (objservice.AdminLogin(uname,upwd))
{
context.Response.Write("你成功了 ");
}
else
{
context.Response.Write("你失败了");
}
效果图