MVC开发程序中,session的用法
public class UserBF { private masterDataContext _Context = new masterDataContext(); public bool Login(string users,string pass) { var query = _Context.denglu.Where(p => p.users == users && p.pass == pass); return query.Count() > 0; } } public ActionResult Checkuidpwd(string uid,string pwd) { bool isok = new UserBF().Login(uid,pwd); if (isok == true) { //保存状态 //把输入的用户名存在Session里 Session["user"] = uid; //跳转界面 return RedirectToAction("Index","Home"); } else { return RedirectToAction("Index"); } } public ActionResult Index() { //如果session里没有值,就证明没有登录,返回登录页面。不能跳过登录页面 if (Session==null) { return RedirectToAction("index","login"); } return View(); } <div>欢迎您:@Session["user"].ToString()</div>