程序目录:
说明:本程序为省事,将 BLL 也写在了 M 中,但 DAL 在别处。
模型 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;
DataTable dt = LTE.DB.IbatisHelper.ExecuteQueryForDataTable("getUser", ht); // Ibatis 数据访问 DAL,自己写的类,较为底层,不列出
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
LoginController.cs:获取页面数据,调用模型中的验证逻辑,并进行相应的处理和页面跳转。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LTE_MVC.Models;
namespace LTE_MVC.Controllers
{
public class LoginController : Controller
{
public ActionResult Index() //呈现视图
{
return View("Login"); // 返回 Login.aspx 页面
}
/// <summary>
/// 验证登陆
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult Login()
{
string userName = Request.Params["userName"]; //接受 Form 提交的数据
string usrPwd = Request.Params["userPwd"];
if (LoginModel.CheckUser(userName, usrPwd))
{
ViewData["loginInfo"] = "欢迎登录!" + userName;
return View("Home");
}
else
{
ViewData["loginInfo"] = "用名或密码错误!";
return View("Login");
}
}
}
}
视图 V
Login.aspx:获取用户输入,点击“登录”后,采用相应的控制器中的方法进行处理。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<LTE_MVC.Models.LoginModel>" %>
<!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 runat="server">
<title>登录</title>
</head>
<body>
<div>
<form method="post" action="/Login/Login">
用户名:<input type="text" name="userName" />
<br /><br />
密码:<input type="password" name="userPwd"/>
<br /><br />
<input type="submit" value="登录" /><br />
<span style="color:red"><%= ViewData["loginInfo"]%></span>
</form>
</div>
</body>
</html>
Home.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!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 runat="server">
<title>Home</title>
</head>
<body>
<div>
<%= ViewData["loginInfo"]%>
</div>
</body>
</html>
配置
global.aspx:配置默认的控制器及处理方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace LTE_MVC
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Login", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
结果
初始页面,Login.aspx:
登录失败,仍然在 Login.aspx:
登录成功,跳转到 Home.aspx:
参考
MVC+三层架构实现简单登录 https://www.jianshu.com/p/c876d6de06fb
mvc2 @ViewData 无法取出数据 https://zhidao.baidu.com/question/680451273442414652.html
下一篇:asp.net web API 非常简单的登录 https://blog.csdn.net/u012319493/article/details/89034874