上一篇:asp.net MVC2 非常简单的登录 https://blog.csdn.net/u012319493/article/details/88992791
感觉 web API 与 MVC 一样,都有 M、V、C,但区别如下:
- MVC 中,C 继承 Controller;web API 中,C 继承 ApiController。
- MVC 中,C 中的 Action 名没有要求;web API 中,C 中的 Action 以 Post、Get、Put、Delete 开头,或者打上接受动词标签 HttpPos、 HttpGet。
- MVC 中,C 返回 V; web API 中,C 不返回 V。
- MVC 中,页面跳转由 C 控制;web API 中,可在 V 上实现页面跳转,但需要借助于 MVC 中的 C。
- web API 中,V 与 C 的分离更为彻底一些
程序目录:
说明:本程序为省事,将 BLL 也写在了 M 中,但 DAL 在上图的目录 DB 中。
M
LoginModel.cs:查询数据库,验证用户名、密码是否正确。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Collections;
using System.Data;
namespace LTE_MVC.Models
{
public class LoginModel
{
/// <summary>
/// 用户名
/// </summary>
public string userName { get; set; }
/// <summary>
/// 密码
/// </summary>
[DisplayName("密码")]
public string userPwd { get; set; }
/// <summary>
/// 检查用户是否合法
/// </summary>
/// <param name="name">用户名</param>
/// <param name="pwd">密码</param>
/// <param name="cityName">用户所属城市名</param>
/// <returns>真 OR 假</returns>
public static bool CheckUser(string name, string pwd)
{
//return true;
//判断用户名密码以及地市名是否匹配
try
{
Hashtable ht = new Hashtable();
ht["name"] = name;
ht["pwd"] = pwd;
// Ibatis 数据访问 DAL,自己写的类,较为底层,不列出
DataTable dt = DB.IbatisHelper.ExecuteQueryForDataTable("getUser", ht);
if (dt.Rows.Count == 0)
{
return false;
}
else
{
return true;
}
}
catch (System.Data.SqlClient.SqlException err)
{
if (err.Message.IndexOf("连接超时") != -1)
{
return false;
}
else if (err.Message.IndexOf("侦听") != -1)
{
return false;
}
else
{
return false;
}
}
catch (System.Exception err)
{
return false;
}
}
}
}
C
HomeController.cs:控制页面跳转。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcWebAPI.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
}
}
LoginController.cs:获取页面数据,调用模型中的验证逻辑。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LTE_MVC.Models;
namespace LTE_MVC.Controllers
{
public class LoginController : ApiController
{
// POST api/login
/// <summary>
/// 验证登陆
/// </summary>
/// <returns></returns>
[HttpPost]
public bool Post([FromBody]LoginModel user)
{
return LoginModel.CheckUser(user.userName, user.userPwd);
}
}
}
V
index.chtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<ul id="menu">
<li> @Html.ActionLink("登录", "Login", "Home")</li>
</ul>
</div>
</body>
</html>
login.chtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
</head>
<body>
<div>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<section>
<h2>用户登录</h2>
用户名:<input type="text" id="userName" value="admin" />
<br /><br />
密码:<input type="password" id="userPwd" value="admin" />
<br /><br />
<input type="button" id="login" value="登录" /><br/>
</section>
</div>
</body>
</html>
<script>
var LoginModel =
{
create: function ()
{
userName: "";
userPwd: "";
return LoginModel;
}
}
$("#login").click(function (){
var user = new LoginModel.create();
user.userName = $("#userName").val();
user.userPwd = $("#userPwd").val();
$.ajax
({
url: "/api/login",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(user),
success: function (data, status)
{
if (data == true)
{
alert("登录成功!");
window.location.href = "@Url.Action("Index","Home")";
}
else
alert("登录失败!");
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert("登录失败,消息:" + textStatus + " " + errorThrown);
}
});
});
</script>
结果
初始页面:
点击登录后:
登录成功:
登录失败:
参考
ASP.NET WEB API入门实例 https://blog.csdn.net/qq_36456952/article/details/62885273
C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解 https://www.cnblogs.com/landeanfen/p/5501487.html