拦截器拦截未登录的请求
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//用于拦截未登录的请求
base.OnActionExecuting(filterContext);
if (Session["user"] == null) {
filterContext.Result = new RedirectResult("/Login/Index");
}
}
}
后面控制器继承上面的基类控制器
验证登录
[HttpPost]
public ActionResult CheckLogin(tbUserInfo userInfo) {
string passwordbymd5= FormsAuthentication.HashPasswordForStoringInConfigFile(userInfo.UserPassword, "md5");
tbUserInfo Users = dbContext.Set<tbUserInfo>().Where(n => n.UserName == userInfo.UserName && n.UserPassword == passwordbymd5).FirstOrDefault();
if (Users == null) {
return Content("<script>alert('用户名或密码错误!');history.go(-1)</script>");
}
else {
Session["user"] = Users;
return RedirectToAction("Index", "Home");
}
}
分页
public class HomeController : BaseController
{
DbContext dbContext = DbContextFactory.instance;
public string pagesize = System.Configuration.ConfigurationManager.AppSettings["pagesize"];
public ActionResult Index()
{
return View();
}
public ActionResult ProfessionalManager(int? page)
{
var tbSpecialties = dbContext.Set<tbSpecialtyInfo>().ToList();
ViewData["SpecialtyInfoList"] = tbSpecialties;
int pageNumber = page ?? 1;
int pageSize = int.Parse(pagesize);
IPagedList<tbSpecialtyInfo> pagedList = tbSpecialties.ToPagedList(pageNumber, pageSize);
return View(pagedList);
}