MVC - M(Model模型层) V(View视图层) C(Control控制层)
数据访问层 界面层 业务逻辑层
WebForm 是请求访问哪一个页面,返回的是一个处理后的页面
MVC 是请求访问的哪一个控制器里面的动作 Controller Action
<%@ %> <% %> <%= %> 作用
<%@ %> 声明语言或引用命名空间
1 <%@ Language="c#"%>
2 <%@ Import NameSpace="System.Data.SqlClient"%>
<% %>放c#代码
<%= %>放一个字段或一个变量
MVC登陆界面,状态保持,退出登录
Controllers文件夹下添加Hone 和Login 两个控制器
1 public class LoginController : Controller 2 { 3 //Login控制器 4 5 public ActionResult Login0()//登陆界面 6 { 7 return View(); 8 } 9 public ActionResult Login1(string username,string pwd)//登陆 10 { 11 Users u = new UsersData().Select(username,pwd);//查询数据 12 if (u != null)//查到数据 13 { 14 //创建Cookies 15 Response.Cookies["un"].Value = u.UserName; 16 //跳转到主界面 17 return RedirectToAction("Index", "Home"); 18 } 19 else//没有数据 20 { 21 // 创建Session,用于传到登录界面错误提示 22 Session["loginok"] = "1"; 23 //跳回登录界面 24 return RedirectToAction("Login0", "Login"); 25 } 26 } 27 }
Home控制器
1 public class HomeController : Controller 2 { 3 4 //Home控制器 5 6 public ActionResult Index()// 主界面 7 { 8 if (Request.Cookies["un"] != null) //Cookies有数据跳转到主界面 9 { 10 return View(); 11 } 12 else // 跳转到登陆界面 13 { 14 return RedirectToAction("Login0","Login"); 15 } 16 } 17 18 public ActionResult Exit() //退出 19 { 20 //设置Cookies 过期时间 21 Response.Cookies["un"].Expires = DateTime.Now.AddDays(-10); 22 //跳转到登录界面 23 return RedirectToAction("Login0","Login"); 24 } 25 }
Views文件夹下Login0视图和Index视图
1 <body> 2 3 <h1>登录界面</h1> 4 <%-- form 表单action提交到那个服务器 method提交方法--%> 5 <form action="Login1" method="post"> 6 用户名:<input type="text" name="username" /><br /> 7 <br /> 8 密码:<input type="password" name="pwd" /><br /> 9 <br /> 10 <%--Session["loginok"]是否有数据用于错误提示--%> 11 <% 12 if (Session["loginok"] != null) 13 { 14 %> 15 <span style="color: red;">用户名密码输入有误!</span> 16 <% 17 } 18 Session["loginok"] = null; 19 %> 20 <input type="submit" value="登录" /> 21 </form> 22 23 </body>
Index视图设置一个退出按钮
1 <form action="Home/Exit" method="post"> 2 <input type="submit" value="退出登录" /> 3 </form>