59-10000 前端和後臺互動基本過程測試 ,舉例說明 輸入正確就登入成功,否則進行一個驗證提示
阿新 • • 發佈:2021-12-09
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 action="Handler/Login.ashx" method="post"> 使用者名稱:<input type="text" name="uname" /> 密碼: <input type="password" name="upwd" /> <input type="submit" name="btnSubmit" value="登入 " /> </form> </body> </html>
一般處理程式 ashx
using System; using System.Collections.Generic; using System.Linq;using System.Web; namespace asp.netTest.Handler { /// <summary> /// Login 的摘要說明 /// </summary> public class Login : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; // 獲取前臺網頁所提交的資料string uname = context.Request.Params["uname"]; string upwd = context.Request.Params["upwd"]; // 呼叫資料訪問方法 判斷使用者名稱或者密碼是否正確 DAL.AdminService objService = new DAL.AdminService(); if (objService.AdminLogin(uname, upwd)) { context.Response.Write("登入成功"); } else { context.Response.Write("使用者名稱或者密碼錯誤!"); } } public bool IsReusable // 是否自動快取此物件 以供下次使用 { get { return false; } } } }
C# 類檔案
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace asp.netTest.DAL { public class AdminService { public bool AdminLogin(string name,string pwd) { // 模擬從資料庫判斷 if (name == "xiaowang" && pwd == "123456") { return true; } else { return false; } } } }
執行效果:
輸入正確就登入成功,否則進行一個驗證提示。