目前在試著了解WebAPI的使用
使用WebForm建立簡單頁面如下
想要在頁面中TextBox輸入帳號密碼後
透過API再將取得到的帳號密碼資料傳回同一頁面顯示
我寫了物件Account 取得帳號與密碼資料
API如下 預想是利用Post傳回帳號密碼資料
但前端的資料卻無法傳遞至API
也不知道該怎麼再利用API傳回資料至前端顯示
前端如下
LogIn.aspx
LogIn.aspx.cs
不好意思
剛學習程式不久
還請各位提點提點謝謝
這是一段處理客戶登入的API(一樣是傳入帳號密碼),提供給你參考
//回傳Format(json type)
{
"status": {
"code": 0,
"message": "success"
},
"data": {
"result": "xxx"
}
}
//[Controllers]
[HttpPost]
public object custlogin([FromBody]request_login req)
{
SqlConnection conn = new SqlConnection(connstr_db);
conn.Open();
signup_root root = new signup_root();
response_status status = new response_status();
signup_data data = new signup_data();
if (req.name.Length>0 && req.pwd.Length>0)
{
string sql = "";
SqlCommand sqlcmd;
sql = "select * from users ";
sql += "where name='" + req.name + "' and pwd='" + req.pwd + "'";
sqlcmd = new SqlCommand(sql, conn);
SqlDataReader dr = sqlcmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
status.code = "0";
status.message = "success";
data.result=req.name+"/"+req.pwd;
root.status = status;
root.data = data;
}
}
else
{
status.code = "-1";
status.message = "Not Found.";
data.result="NULL";
root.status = status;
root.data = null;
}
dr.Close();
dr.Dispose();
sqlcmd.Dispose();
}
else
{
status.code = "-2";
status.message = "Input Error!!";
data.result="NULL";
root.status = status;
root.data = null;
}
conn.Close();
conn.Dispose();
return root;
}
//[Models]
//接收傳入的帳密
public class request_login
{
public string name { get; set; }
public string pwd { get; set; }
}
//回傳格式
public class signup_root
{
public response_status status { get; set; }
public signup_data data { get; set; }
}
public class response_status
{
public string code { get; set; }
public string message { get; set; }
}
public class signup_data
{
public string result { get; set; }
}